【问题标题】:Binding the title of a LayoutDocument from a collection in AvalonDock 2.0从 AvalonDock 2.0 的集合中绑定 LayoutDocument 的标题
【发布时间】:2015-02-12 16:26:07
【问题描述】:

我将 ObservableCollection 绑定到 AvalonDock 2.0,其中集合中的每个项目都是 AvalonDock 文档。这就是我进行绑定的方式:

<ad:DockingManager DocumentsSource="{Binding Path=OpenProjects, Mode=TwoWay}" ActiveContent="{Binding Path=CurrentProject, Mode=TwoWay}" LayoutItemTemplateSelector="{StaticResource ProjectTemplateSelector}">
...
</ad:DockingManager>

问题是我想将每个项目的名称(在CurrentProject 中的属性Name 中指定)显示为文档标题。这是我尝试过的:

<ad:DockingManager.DocumentHeaderTemplate>
    <DataTemplate>
        <TextBlock DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ad:DockingManager}, Path=ActiveContent, Mode=OneWay}" Text="{Binding Path=Name}" />
    </DataTemplate>
</ad:DockingManager.DocumentHeaderTemplate>

如果我只打开一个文档,这可以正常工作,但是当我打开多个文档时,它们都会显示当前项目的Name。例如,如果我有四个打开的项目,名称分别为“A”、“B”、“C”和“D”,如果我当前正在查看文档“C”,则所有四个选项卡都将显示标题“C” ",而当我更改为文档“B”时,它们都会将其名称更改为“B”。

有没有办法防止这种变化?我尝试将绑定模式设置为OneTime,但似乎不起作用。

【问题讨论】:

    标签: c# wpf mvvm avalondock


    【解决方案1】:

    我最终做了这么简单的事情:

    <ad:DockingManager.DocumentHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Content.Name}" />
        </DataTemplate>
    </ad:DockingManager.DocumentHeaderTemplate>
    

    说明: DocumentHeaderTemplate 内绑定的DataContext 是LayoutDocument 本身。原来它有一个名为 Content 的属性,它表示每个文档中的 de binding 对象(在这种情况下,我的 OpenProjects 集合中的每个 Project)。在那个对象中,我有属性 Name,它是我想用于标题的字符串。

    【讨论】:

    • 不错!作为对此感到困惑的人的参考,可用模板映射到以下含义:AnchorableTitleTemplate =(停靠)工具窗口,DocumentHeaderTemplate = 文档窗口/编辑器,DocumentPaneMenuItemHeaderTemplate = 右侧下拉菜单中的项目选项卡标题,DocumentTitleTemplate = 浮动文档窗口。
    【解决方案2】:

    这是因为您将标题文本绑定到通过停靠管理器的ActiveContent 引用的对象的属性。很明显,更改ActiveContent(聚焦文档)会将所有LayoutDocument 视图的标题更新为相同的值,因为所有标题都绑定到相同的源。

    你可以试试这样的:

    <ad:DockingManager.DocumentHeaderTemplate>
        <DataTemplate>
            <Border x:Name="Header">
                <ad:AnchorablePaneTitle Model="{Binding Model, RelativeSource={RelativeSource TemplatedParent}}"/>
            </Border>
        </DataTemplate>
    </ad:DockingManager.DocumentHeaderTemplate>
    

    【讨论】:

    • 虽然不是正确答案,但这帮助我指出了正确的方向(请参阅我自己发布的答案)。
    猜你喜欢
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 2017-04-12
    • 2012-10-19
    • 1970-01-01
    相关资源
    最近更新 更多