I’m using this ObservableCollection-Class within my Project: Link
I want to Bind a RibbonMenuButton to a ObservableDictionary<string,bool>:
<r:RibbonMenuButton ItemsSource="{Binding MyDictionary}">
<r:RibbonMenuButton.ItemContainerStyle>
<Style TargetType="{x:Type r:RibbonMenuItem}">
<Setter Property="IsCheckable" Value="true"/>
<Setter Property="Header" Value="{Binding Path=Key}"/>
<Setter Property="IsChecked" Value="{Binding Path=Value}"/>
</style>
</r:RibbonMenuButton.ItemContainerStyle>
</r:RibbonMenuButton>
But I get exceptions because the Value-Properties of the internal IDictionary-KeyValuePairs are readonly. Any Idea how to solve this?
I thought about something like:
<Setter Property="IsChecked" Value="{Binding Source=MyDictionary[{Binding Path=Key}]}"/>
But this won’t work ’cause of {Binding} in {Binding}…
This doesn’t work, because your dictionary isn’t treated as a dictionary but as an
IEnumerable<KeyValuePair<string, bool>>. So eachRibbonMenuItemis bound to aKeyValuePair<string, bool>with readonly propertiesKeyandValue.You can do
twoone things:1. Use anObservableCollection<Tuple<string, bool>>instead of the dictionary and bindIsCheckedtoItem2.2. Create a little helper class that contains a
IsCheckedproperty and change your dictionary to contain that class as the value and bindIsCheckedtoValue.IsChecked.I would go with answer two, because the needed changes and possible side effects are smaller.
My answer assumes that you want to have a two way binding on
IsChecked. If not, go with the answer of slugster.