Caliburn Micro简单MDI

时间:2011-02-07 17:44:01

标签: wpf mdi caliburn.micro

您好我在caliburn micro中创建simpel MDI,如下所示:http://devlicio.us/blogs/rob_eisenberg/archive/2010/10/19/caliburn-micro-soup-to-nuts-part-6c-simple-mdi-with-screen-collections.aspx

每个标签项都由ID标识(ID为DisplayName propety)。我需要为每个id.Tab项打开只有一个标签项是用户控件。

标签项视图模型类位于:

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

因此,如果我在shell中激活标签项,我将标签ID存储在列表中。

当标签项停用时,我需要从列表中删除标签ID。

Shell视图模型类:

[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视图:

<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>

我可以使用事件聚合器类实现此行为,并从shell视图模型类的选项卡视图模型类发布消息 何时禁用标签项。

但我想用更简单的东西。例如,选项卡项可以在停用时调用shell视图方法。

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:1)

您继承Screen的{​​{1}}类(由框架提供)定义方法TabChatViewModel。此方法尝试通过询问其父(在您的情况下将是TryClose指挥)或要求视图关闭来关闭当前屏幕。

所以你需要做的就是在调用关闭操作时从ChatShellViewModel内调用TryClose()

相关问题