I have a Contextmenu with a ListBox inside. Whenever I right click my control and select a value from my contextmenu the last selected value stays marked and I can’t select that value again.
The idea is, that I may select the same value within the COntextmenu in order to turn a property on or off.
Should be quite basic, what am I missing?
Many Thanks,
EDIT: Thanks for the responses. I have tried to apply your ideas without success. I think the major problem is that MenuItems of a contextmenu have no ItemSource to be bound to a collection (e.g. PossibleValues as in this example).
May I insert my code for clarification:
<Border.ContextMenu>
<ContextMenu>
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property ="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<ContentPresenter x:name="Header" ContentSource="Header" RecognizesAccessKey="True"/>
</ControlTemplate>
</Setter.value>
</Setter>
</Style>
<ContextMenu.ItemContainerStyle>
<ListBox BorderThickness="0" Width="35" Margin="0"
SelectedItem="{Binding Path=Volume, Mode=TwoWay}
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
ItemsSource="{Binding Path=PossibleValues}"/>
</ContextMenu>
</Border.ContextMenu>
Why do you have a ListBox in your ContextMenu? A ListBox allows you to select one item out of a list (or multiple items, if enabled). An example is the list of files in Explorer.
A ContextMenu is an ItemsControl, ie. you can add an arbitrary number of items to it. So just add the items directly.
EDIT: Both the ContextMenu and MenuItem are ItemsControl. They both have an ItemsSource property. So you could have the following:
PossibleValues could be a collection of MenuItems. Each menuitem could look at follows (here in code), for example:
EDIT2: Try something like the following. As command you could use one of the many implementations from MVVM helper libraries, such as DelegateCommand<> from Prism or SimpleCommand from Cinch.
As for your execute handler of the command, something along the lines of: