带Caliburn.Micro的WPF动态上下文菜单 [英] WPF Dynamic Context Menu with Caliburn.Micro

查看:94
本文介绍了带Caliburn.Micro的WPF动态上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在基于Caliburn.Micro的应用程序中创建动态上下文菜单。任何人都可以分享一个有效的方法示例吗?
到目前为止,我对每个上下文菜单项都有一个非常小的模型:

I am trying to create a dynamic context menu in my Caliburn.Micro based application. Can anyone share an example of an effective way to do that? So far, I have a very minimal model for each context menu item:

public class ContextMenuUiModel
{
    public string Name { get; set; }
}

我的视图模型中的一个属性,显示这些菜单项模型的列表:

a property in my view model that presents a list of those menu item models:

      BindableCollection<ContextMenuUiModel> m_ContextMenuItems = new BindableCollection<ContextMenuUiModel>
  {
     new ContextMenuUiModel { Name="Item 1"},
     new ContextMenuUiModel { Name="Item 2"},
     new ContextMenuUiModel { Name="Item 3"}
  };
  public BindableCollection<ContextMenuUiModel> ContextMenuItems
  {
     get {return m_ContextMenuItems;}
  }

以及一个以集合属性命名的菜单项(基于FreePIE中的菜单创建,可通过问题和答案

and, a menu item named for the collection property (based on the menu creation in FreePIE, found via this question and answer)

         <TreeView  x:Name="ConfigItemTree" VerticalAlignment="Top" ItemsSource="{Binding ConfigTreeRoot}" >
           <TreeView.ContextMenu>
             <ContextMenu >
                <MenuItem x:Name="ContextMenuItems" DisplayMemberPath="Name" />
             </ContextMenu>
         </TreeView.ContextMenu>

Caliburn.Microloging报告 get_ContextMenuItems没有可操作元素。同样,尽管Caliburn注意没有找到其属性的其他命名元素(例如,未应用绑定约定:Element ConfigItemTree与属性不匹配。),但它并未对ContextMenuItems做出类似声明。因此,看来Caliburn只是没有将ContextMenu视为可以或应该处理的元素。

Caliburn.Micro logging reports "No actionable element for get_ContextMenuItems". Also, although Caliburn is noting other named elements for which no property was found (e.g. "Binding Convention Not Applied: Element ConfigItemTree did not match a property."), it is not making a similar statement for ContextMenuItems. So, it seems Caliburn is just not seeing the ContextMenu as an element it could or should deal with.

也许问题在于Caliburn无法看到上下文菜单,因为它只有在右键单击后才真正存在(类似于此问题已崩溃)元素)?

Maybe the issue is that Caliburn can't see the context menu because it doesn't actually exist until a right click happens (similar to this issue with collapsed elements)?

最终,我希望上下文菜单的内容基于右键单击的树视图项目,可能包括子菜单和/或禁用的物品。不过,起初,我会满足于可获得的所有物品。

Ultimately, I would like the context menu's contents to be based on the tree view item that was right clicked, possibly including sub menus and/or disabled items. For a start, though, I'll settle for whatever items I can get.

推荐答案

绑定 ContextMenu 的I​​temsSource 属性到 ContextMenuItems 属性的

Bind the ItemsSource property of the ContextMenu to the ContextMenuItems property:

<ContextMenu ItemsSource="{Binding PlacementTarget.DataContext.ContextMenuItems, RelativeSource={RelativeSource Self}}" 
             DisplayMemberPath="Name" />

这篇关于带Caliburn.Micro的WPF动态上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆