Caliburn.Micro 的 WPF 动态上下文菜单
WPF Dynamic Context Menu with Caliburn.Micro
我正在尝试在基于 Caliburn.Micro 的应用程序中创建动态上下文菜单。任何人都可以分享一个有效方法的例子吗?
到目前为止,我对每个上下文菜单项都有一个非常小的模型:
public class ContextMenuUiModel
{
public string Name { get; set; }
}
a 属性 在我的视图模型中显示了这些菜单项模型的列表:
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 中的菜单创建,通过 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.Micro 记录报告 "No actionable element for get_ContextMenuItems"。此外,虽然 Caliburn 注意到没有找到 属性 的其他命名元素(例如 "Binding Convention Not Applied: Element ConfigItemTree did not match a property."),但它没有对 ContextMenuItems 做出类似的声明。因此,Caliburn 似乎只是没有将 ContextMenu 视为它可以或应该处理的元素。
也许问题是 Caliburn 看不到上下文菜单,因为它实际上并不存在,直到发生右键单击(类似于 this issue with collapsed elements)?
最终,我希望上下文菜单的内容基于右键单击的树视图项目,可能包括子菜单 and/or 禁用项目。不过,首先,我会满足于我能得到的任何物品。
将ContextMenu
的ItemsSource
属性绑定到ContextMenuItems
属性:
<ContextMenu ItemsSource="{Binding PlacementTarget.DataContext.ContextMenuItems, RelativeSource={RelativeSource Self}}"
DisplayMemberPath="Name" />
我正在尝试在基于 Caliburn.Micro 的应用程序中创建动态上下文菜单。任何人都可以分享一个有效方法的例子吗? 到目前为止,我对每个上下文菜单项都有一个非常小的模型:
public class ContextMenuUiModel
{
public string Name { get; set; }
}
a 属性 在我的视图模型中显示了这些菜单项模型的列表:
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 中的菜单创建,通过 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.Micro 记录报告 "No actionable element for get_ContextMenuItems"。此外,虽然 Caliburn 注意到没有找到 属性 的其他命名元素(例如 "Binding Convention Not Applied: Element ConfigItemTree did not match a property."),但它没有对 ContextMenuItems 做出类似的声明。因此,Caliburn 似乎只是没有将 ContextMenu 视为它可以或应该处理的元素。
也许问题是 Caliburn 看不到上下文菜单,因为它实际上并不存在,直到发生右键单击(类似于 this issue with collapsed elements)?
最终,我希望上下文菜单的内容基于右键单击的树视图项目,可能包括子菜单 and/or 禁用项目。不过,首先,我会满足于我能得到的任何物品。
将ContextMenu
的ItemsSource
属性绑定到ContextMenuItems
属性:
<ContextMenu ItemsSource="{Binding PlacementTarget.DataContext.ContextMenuItems, RelativeSource={RelativeSource Self}}"
DisplayMemberPath="Name" />