3

In my WPF Caliburn.Micro application, I have a TabControl. I want to be able to close tabs as needed. I found a way to do it here:

http://devlicio.us/blogs/rob_eisenberg/archive/2010/10/19/caliburn-micro-soup-to-nuts-part-6c-simple-mdi-with-screen-collections.aspx

But when I try to apply it, I get an error:

No target found for method CloseItem.

Here is my code:

   <TabControl x:Name="Items" >
    <TabControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding DisplayName}" />
                <Button Content="X"  
                            cal:Message.Attach="CloseItem($dataContext)" />
            </StackPanel>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

Could you please help?

Thanks.

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
David Shochet
  • 5,035
  • 11
  • 57
  • 105

2 Answers2

7

Another way of doing it would be:

<Button Content="X" cal:Message.Attach="DeactivateItem($dataContext, 'true')" />

that way you don't have to create an extra method.

gcores
  • 12,376
  • 2
  • 49
  • 45
  • Yes. The problem seems to be current Caliburn.Micro doesn't have CloseItem defined as part of IConductor interface, so using DeactivateItem and passing in true to close is a good workaround for the tutorial case. – Eternal21 Apr 18 '16 at 16:47
  • The official sample in git seems to use the same solution: https://github.com/paulcbetts/CaliburnMicro/blob/master/samples/Caliburn.Micro.SimpleMDI/Caliburn.Micro.SimpleMDI/ShellView.xaml – Eternal21 Apr 19 '16 at 11:57
2

Not entirely certain what's causing your exact issue (I suspect a very old tutorial coupled with a vastly different assembly is the issue) but I know how you can get it to work. Create any old public method like this and call it. It's going to override what you've got in there so you could name it CloseItem. Then call this extension method and pass in the IConductor instance and the IScreen instance.

public void CloseItem(object dataContext)
{
    ScreenExtensions.CloseItem(this, dataContext);
}
erodewald
  • 1,815
  • 20
  • 45
  • I wonder if you know how to answer some other questions I have: http://stackoverflow.com/questions/11852418/componentones-flexgrid-background-color and http://stackoverflow.com/questions/11835186/c1flexgrid-width Thanks! – David Shochet Aug 08 '12 at 17:11
  • Downloaded the C1 trial and took a look -- I posted a solution for you as a comment, then moved it to an answer. Hope it helps. – erodewald Aug 08 '12 at 18:01