1
votes

Hi I create simpel MDI in caliburn micro something like this : http://devlicio.us/blogs/rob_eisenberg/archive/2010/10/19/caliburn-micro-soup-to-nuts-part-6c-simple-mdi-with-screen-collections.aspx.

Every tab item is identified by ID (ID is DisplayName propety). I need have opened only single tab item for every id.Tab item is user control.

Tab item view model class is here:

[Export(typeof(ITabChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class TabChatViewModel : Screen, IViewModelIdentity,
    IPartImportsSatisfiedNotification,
    ITabChatViewModel, IHandle<IRp>, IHandle<IDetailData>
{...}

So if I activate tab item in shell I store tab ID in list.

I need something remove tab ID from list when is tab item deactivate.

Shell view model class:

[Export(typeof(IChatShellViewModel))]
public class ChatShellViewModel : 
    Conductor<IScreen>.Collection.OneActive, 
    IChatShellViewModel
{
    //consist active tab item
    List<string> ActiveTabItems { get; set; }

    public ChatShellViewModel()
    {
        ActiveTabItems=new List<string>();
    }

    public void OpenChatTab(IScreen screen)
    {
        if(!ActiveTabItems.Contains(screen.DisplayName))
        {
            ActivateItem(screen);
           ActiveTabItems.Add(screen.DisplayName);
        }

    }


}

Shell view:

<Window x:Class="Spirit.Views.ChatShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
        Height="545" 
        Width="680">
    <DockPanel>
        <TabControl x:Name="Items">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding DisplayName}"  
                                   VerticalAlignment="Center"/>
                        <Image Source="/images/icons/close.png" 
                                       Margin="8,4,4,4" 
                                       Height="16" 
                                       Width="16"
                                       HorizontalAlignment="Right"
                                       VerticalAlignment="Center"
                                       cal:Message.Attach="[Event MouseLeftButtonDown]=[Action CloseItem($dataContext)]"/>
                    </StackPanel>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>
    </DockPanel>
</Window>

I can achieve this behavior with event aggregator class and publish message from tab item view model class on shell view model class when is tab item deactivated.

But I would like use something more simple. For example tab item can call shell view method when is deactivated.

Any advice? Thank

1
You should use button instead of image+mousedown (and then style it flat) because it handles touch, stylus and mouse interaction with the "Click" event - it also handles the scenario where mousedown happens on the button but mouseup doesnt. - Rhys Bevilaqua

1 Answers

1
votes

The Screen class (which is provided by the framework) that you are inheriting TabChatViewModel from, defines the method TryClose. This method attempts to close the current screen by either asking its Parent (which in your case will be the ChatShellViewModel conductor) or asking the view to close.

So all you need to do is call TryClose() from within the TabChatViewModel when the close operation is invoked.