简体   繁体   English

上下文菜单,取决于列表框项目类型

[英]Context menu depending on Listbox item type

Is it good idea to select ContextMenu of ListBox item through Converter or is there some better way to do it? 通过Converter选择ListBox项的ContextMenu是个好主意,还是有一些更好的方法呢?

Just to be clear I will describe my situation: 为了清楚起见,我将描述我的情况:

I have a Listbox control. 我有一个列表框控件。 As a ItemsSource of this Lisbox is ObservableCollection<MyClass> 作为此Lisbox的ItemsSource ,是ObservableCollection<MyClass>

I've set ContextMenu of ItemContainerStyle of the Listbox to look like this : 我已经设置了Listbox的ItemContainerStyle的ContextMenu看起来像这样:

<Setter Property="ContextMenu" Value="{Binding Converter={StaticResource contextMenuConverter}}"/ >

In ContextMenuConverter I check some properties of MyClass class and select ContextMenu accordingly. ContextMenuConverter中,我检查MyClass类的某些属性,并相应地选择ContextMenu

Uh, no, thats actually pretty good. 嗯,不,实际上不错。

Another option would be to bind the SelectedItem to your ViewModel , bind the ContextMenu property to the same, and return a new ContextMenu whenever the SelectedItem is changed, based on the SelectedItem . 另一种选择是将绑定SelectedItem到您的ViewModel ,绑定ContextMenu属性设置为相同,并返回一个新ContextMenu每当SelectedItem改变的基础上SelectedItem

But, frankly, I like your solution more. 但是,坦率地说,我更喜欢您的解决方案。 Its cleaner. 它的清洁剂。

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

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