简体   繁体   English

在资源ItemsSource中绑定上下文菜单

[英]Bind context menu in resources ItemsSource

In my resources, I have created a Context menu and I want to bind it's ItemsSource property to an ObservableCollection<object> property called ContextMenuItems that I've created in my xaml.cs that is filled with items that I use to represent menu items. 在我的资源中,我创建了一个上下文菜单,我想将其ItemsSource属性绑定到我在xaml.cs中创建的名为ContextMenuItemsObservableCollection<object>属性,该属性充满了我用来表示菜单项的项目。 Currently, it looks like this 目前看起来像这样

<UserControl.Resources>
    <ContextMenu x:Key="ContextMenu"
             DataContext="{Binding}"
             ItemsSource="{Binding ContextMenuItems}">
       <ContextMenu.Resources>
           <DataTemplate DataType="{x:Type local:CtContextMenuItem}">
                   <MenuItem Header="{Binding Header}"
                          Click="MenuItem_OnClick"
                          Visibility="{Binding Visibility}"
                          IsEnabled="{Binding IsEnabled}" />
           </DataTemplate>
           <DataTemplate DataType="{x:Type local:CtContextMenuSepparatorItem}">
               <Separator />
           </DataTemplate>
       </ContextMenu.Resources>
   </ContextMenu>
</UserControl.Resources>

and then in my treview 然后在我的视野中

<TreeView ContextMenu="{DynamicResource = ContextMenu}">
   <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type local:CustomizableTreeBaseItem}"
                                ItemsSource="{Binding TreeChildren}">
          <TextBlock Text="{Binding Header}" 
                     ContextMenu="{DynamicResource = ContextMenu}/>
       </HierarchicalDataTemplate>
     </TreeView.Resources>
</TreeView>

TreeChildren is a public Property on the local:CustomizableTreeBaseItem that contains public properties that are then bound to the items of the tree-view. TreeChildrenlocal:CustomizableTreeBaseItem上的公共属性,其中包含公共属性,这些公共属性随后绑定到树视图的项目。 An Observable collection of local:CustomizableTreeBaseItem is set as to the Items property of the tree-view from code-behind. 从代码隐藏开始,将树形视图的Items属性设置为local:CustomizableTreeBaseItem的Observable集合。

But this does not seem to work when I right-click items in my tree-view I don't get a context menu as I expect. 但这在我右键单击树状视图中的项目时似乎不起作用,但我没有获得期望的上下文菜单。
I have tried to look up if I need to create a resource that somehow is bound to the property but it does not seem like there is a way to that. 我尝试查找是否需要创建某种以某种方式绑定到该属性的资源,但是似乎没有办法解决该问题。 I have also tried to Bind the data context but I can't seem to come up with a path that would work. 我也尝试过绑定数据上下文,但似乎无法提出一条可行的路径。

If the ContextMenuItems property is defined in the code-behind of the UserControl where the ContextMenu resource is defined, you could specify a RelativeSource : 如果ContextMenuItems属性是在定义ContextMenu资源的UserControl的代码背后定义的,则可以指定RelativeSource

<ContextMenu x:Key="ContextMenu"
             ItemsSource="{Binding ContextMenuItems, RelativeSource={RelativeSource AncestorType=UserControl}}">
    <ContextMenu.Resources>
        ...
    </ContextMenu.Resources>
</ContextMenu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM