C
C
Curly Brace2015-11-07 18:29:19
WPF
Curly Brace, 2015-11-07 18:29:19

How to access MenuItem in ContextMenu?

My application shows a tray icon, when clicked, a context menu is displayed in which the first 4 items IsCheckable=True.
But I can't figure out how to check them and remove them in code-behind.
I can’t find anything in Google other than data binding, but in my scenario, the checkbox should be on only one of the 4 items and I thought to switch them in the code, but I don’t know how to access them from the code.
Or am I reinventing the wheel and in WPF there is functionality for such menus with switchable items?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2015-11-08
@stasuss

I can’t find anything in Google other than data binding, but in my scenario, the checkbox should be on only one of the 4 items and I thought to switch them in the code, but I don’t know how to access them from the code.

In codebehind, of course, you can do it, but you don’t need to. You could make a normal viewmodel with four properties corresponding to your menu items and the modes you switch. After that, you simply bind the IsChecked of each menu item to each property of the viewmodel. And already in it you implement the logic that if one item is selected, i.e. some property of the 4 available is set to true, then you remove true from all the others. Of course, in order for the binding to receive the necessary notifications, you must implement INotifyPropertyChanged.
If the above is vague for you, then I can advise what to read, because. your solution is far from the basic ideas of WPF and MVVM :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question