Joh*_*ara 4 c# wpf mvvm avalondock
我将ObservableCollection绑定到AvalonDock 2.0,其中集合中的每个项目都是AvalonDock Document.这是我如何进行绑定:
<ad:DockingManager DocumentsSource="{Binding Path=OpenProjects, Mode=TwoWay}" ActiveContent="{Binding Path=CurrentProject, Mode=TwoWay}" LayoutItemTemplateSelector="{StaticResource ProjectTemplateSelector}">
...
</ad:DockingManager>
Run Code Online (Sandbox Code Playgroud)
问题是我想要显示每个项目的名称(在属性Name中指定CurrentProject)作为文档标题.这就是我尝试过的:
<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>
Run Code Online (Sandbox Code Playgroud)
如果我只打开一个文档,这可以正常工作,但是当我有几个文档时,它们都会显示Name当前项目.例如,如果我有四个打开的项目,名称为"A","B","C"和"D",如果我当前正在查看文档"C",则所有四个选项卡都将显示标题"C" ",当我改为文件"B"时,他们都将名字改为"B".
有没有办法阻止这种变化?我已经尝试将绑定模式设置为OneTime,但它似乎不起作用.
Joh*_*ara 15
我最终做的就像这样简单:
<ad:DockingManager.DocumentHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content.Name}" />
</DataTemplate>
</ad:DockingManager.DocumentHeaderTemplate>
Run Code Online (Sandbox Code Playgroud)
说明: DocumentHeaderTemplate内部绑定的DataContext是LayoutDocument本身.事实证明它有一个名为Content的属性,它表示每个文档中的de binding对象(在本例中,是我的OpenProjects集合中的每个Project).在那个对象中,我有了属性Name,这是我想用于标题的字符串.