ContextMenu和Hierachicaldatatemplate绑定 [英] ContextMenu and Hierachicaldatatemplate Binding
问题描述
在主窗口中,我有一个ObservableCollection的"parent"(父).项目.每个父母都有一个ObservableCollection的"device".项目.
In the MainWindow I have an ObservableCollection of "parent" items. Every parent, has an ObservableCollection of "device" items.
我的ContextMenu的第一级已正确绑定到第一个Collection的项目.问题是,对于每个MenuItem,我总是得到相同的结果(意味着每个MenuItem的数据上下文始终是相同的父"?)
The first level of my ContextMenu is properly bind to the items of the first Collection. The problem is, for every MenuItem I am getting always the same results (meaning that the datacontext for each MenuItem is always the same "parent"?)
为了更好地理解,上下文菜单看起来像这样(对不起,我无法附加任何图片或链接)
To better understand, the contextmenu looks like this (sorry, I can't attach any picture or link)
MenuItem1 -> SubItemA
MenuItem2 SubItemB
SubItemC
________________________
MenuItem1
MenuItem2 -> SubItemA
SubItemB
SubItemC
我打开的每个MenuItem都会得到相同的SubItems.
Every MenuItem I open, I always get the same SubItems.
这是XAML代码:
<StackPanel.Resources>
<CollectionViewSource Source="{Binding Parents}" x:Key="Parents"/>
<HierarchicalDataTemplate DataType="{x:Type local:parent}">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.Source>
<CompositeCollection>
<MenuItem Header="ParentStartAll"/>
<MenuItem Header="ParentStopAll"/>
<MenuItem Header="ParentRestartAll"/>
<Separator/>
<CollectionContainer Collection="{Binding Childs, Source={StaticResource Parents}}"/>
</CompositeCollection>
</Binding.Source>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:device}">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.Source>
<CompositeCollection>
<MenuItem Header="StartAll"/>
<MenuItem Header="StopAll"/>
<MenuItem Header="RestartAll"/>
<Separator/>
<CollectionContainer Collection="{Binding Processes, Source={StaticResource Parents}}"/>
</CompositeCollection>
</Binding.Source>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
[...]
</StackPanel.Resources>
<StackPanel.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding Parents}">
</ContextMenu>
</StackPanel.ContextMenu>
我究竟做错了什么?另外,当我尝试第二次打开ContextMenu时,它仅显示第一级(仅父母").
What am I doing wrong? Also, when I try to open the ContextMenu for the second time, it only shows the first level (only the "parents").
有提示吗?
这里的新手很好,所以如果问题太简单或我犯了一些丑陋的错误,请不要生气.我不得不说,首先要管理绑定并不容易,而且我正在努力使此上下文菜单起作用.
Pretty newbie here, so please don't angry if the question is too easy or if I made some ugly mistakes. I have to say it was not easy to manage the binding in the firstplace and I am struggling to get this contextmenu working.
非常感谢.
推荐答案
>>问题是,对于每个MenuItem,我总是得到相同的结果(这意味着数据上下文每个MenuItem始终是相同的父项"?)
那是因为您要绑定到完全相同的父母"模板中的CollectionViewSource.此资源已定义 一次在StackPanel中,并且将始终返回相同的集合:
That's because you are binding to the exact same "Parents" CollectionViewSource in your templates. This resource is defined once in the StackPanel and will always return the same collection:
<CollectionViewSource Source="{Binding Parents}" x:Key="Parents"/>
如果要创建分层的ContextMenu,则父"菜单将显示为父".该类应该具有一个collection属性,该属性保存所有该特定父级的所有设备,并将HierarchicalDataTemplate的ItemsSource属性绑定到该属性:
If you want to create an hierarchial ContextMenu the "parent" class should have a collection property that holds all of this particular parent's devices and bind the ItemsSource property of the HierarchicalDataTemplate to this property:
<HierarchicalDataTemplate DataType="{x:Type local:parent}" ItemsSource="{Binding Devices}">
...
如果要在HierarchicalDataTemplate中使用CompositeCollection,则应该可以使用转换器来执行此操作.请参考以下线程以获取更多信息和示例:
http://stackoverflow.com/questions/5789149/wpf-complex-hierarchical-data-template
If you want to use a CompositeCollection in your HierarchicalDataTemplate you should be able to do this using a converter. Please refer to the following thread for more information and an example:
http://stackoverflow.com/questions/5789149/wpf-complex-hierarchical-data-template
希望有帮助.
如果您需要进一步的帮助,请提供完整的可复制示例,包括所有相关代码段,类和示例数据.
Please provide a full reproducible sample of your issue including all relevant code snippets, classes and sample data if you need any further help on this.
还请记住通过将有用的帖子标记为答案来关闭话题,如果您有新问题,请重新开始话题.请不要在同一线程中问几个问题.
Please also remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
这篇关于ContextMenu和Hierachicaldatatemplate绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!