I have following class which I use for radio button binding
public class RadioButtonSwitch : ViewModelBase
{
IDictionary<string, bool> _options;
public RadioButtonSwitch(IDictionary<string, bool> options)
{
this._options = options;
}
public bool this[string a]
{
get
{
return _options[a];
}
set
{
if (value)
{
var other = _options.Where(p => p.Key != a).Select(p => p.Key).ToArray();
foreach (string key in other)
_options[key] = false;
_options[a] = true;
RaisePropertyChanged("XXXX");
else
_options[a] = false;
}
}
}
XAML
<RadioButton Content="Day" IsChecked="{Binding RadioSwitch[radio1], Mode=TwoWay}" GroupName="Monthly" HorizontalAlignment="Left" VerticalAlignment="Center" />
ViewModel
RadioSwitch = new RadioButtonSwitch(
new Dictionary<string, bool> {{"radio1", true},{"radio2", false}}
);
I’m having problem with RaisePropertyChanged() in my Class. I’m not sure what value I should be putting in order to raise the change.
I tried putting:
- Item[]
- a
- [a]
I keep getting following error:

This is so in case of any change I can handle it in my view accordingly. Please do not give me solutions for List for radio buttons etc.
The problem is that you are implementing an indexer, not an ordinary property. Although the binding subsystem supports indexers, MVVMLight and
INotifyPropertyChangeddo not.If you want to use an indexer you need to:
ObservableCollection<T>INotifiyCollectionChangedand raise that event insteadThe first option isn’t realistic because you are already deriving from
ViewModelBaseand have to continue to do that. Since implementingINotifiyCollectionChangedis a little bit of work, the easiest approach is to:RadioButtonSwitchthat is an observable collection of boolean values (ObservableCollection<bool>)Then change you binding to add one more path element and you are done.
Edit:
Based on your comment and rereading your question, I think implementing
INotifyCollectionChangedis the easiest. Here is the rewrite of yourRadioButtonSwitchclass which actually no longer needs to derive from the MVVMLight base class, although you still could if you wanted to.The careful reader will notice that we use a sledgehammer and “reset” the whole collection when any element of the collection is modified. This is not just laziness; it is because the indexer uses a string index instead of an integer index and
INotifyCollectionChangeddoesn’t support that. As a result, when anything changes we just throw up our hands and say the whole collection has changed.