简体   繁体   中英

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?

Just to be clear I will describe my situation:

I have a Listbox control. As a ItemsSource of this Lisbox is ObservableCollection<MyClass>

I've set ContextMenu of ItemContainerStyle of the Listbox to look like this :

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

In ContextMenuConverter I check some properties of MyClass class and select ContextMenu accordingly.

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 .

But, frankly, I like your solution more. Its cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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