c# - 将上下文菜单从 App.xaml 附加到 Setter 值

标签 c# wpf contextmenu

我在 App.xaml 中有一个 ContextMenu,如下所示:

<ContextMenu x:Key="TreeContextMenuTest" ItemsSource="{Binding ContextMenu}">....

然后我有一个 ListView,其项目在 Themes/Generic.xaml 中设置样式。它看起来像这样(我已经删除了不相关的东西。样式应用正确,我的问题只是关于如何附加上下文菜单)

<Style TargetType='{x:Type ListViewItem}'>
    <Setter Property="ContextMenu" Value="{StaticResource TreeContextMenuTest}" />
</Style>

但是,我确实收到 DependencyProperty.UnsetValue is not a valid value for property ContextMenu 错误。有什么想法吗?

最佳答案

正如评论中提到的,使用DynamicResource而不是StaticResource -

<Setter Property="ContextMenu" Value="{DynamicResource TreeContextMenuTest}" />

引用此内容 - StaticResource vs DynamicResource

关于c# - 将上下文菜单从 App.xaml 附加到 Setter 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16870235/

相关文章:

wpf - 为什么XAML Margin不遵循CSS规范?

Python:获取资源管理器中选定文件的列表(Windows 7)

c# - 使用字符串格式显示最多 2 位的小数或简单整数

c# - 为什么 VB 编译器不能捕获对接口(interface)类型的无效隐式转换,而 C# 可以?

c# - CollectionViewSource 实时排序不起作用

c# - Wpf:在多个控件上应用自定义样式的工具提示

java - 为什么不调用所选上下文项上的函数

jquery - 创建 jQueryUI 1.8 按钮菜单

c# - 数字和斜杠的正则表达式

c# - Xunit - 在少数全套测试中禁用并行性