Silverlight checkbox two way binding does not work as expected
I have a simple issue setting a two way checkbox binding in Silverlight 3.0. It must be tricky, but maybe I forgot my brain today ...
I have defined a Model class to represent my ... data. I have implemented the INotifyPropertyChanged interface so that the UI can see when the data changes.
public class Model : INotifyPropertyChanged
{
private bool _value;
public bool Value
{
get { return this._value; }
set
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
this._value = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Then I'll put a checkbox and a button on .. 'form':
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="check" IsChecked="{Binding Value, Mode=TwoWay}" Content="SomeLabel"/>
<Button Click="Button_Click" Content="Test" />
</StackPanel>
Then I provided the data in the constructor:
public MainPage()
{
InitializeComponent();
this.DataContext = new Model() { Value = true };
}
The problem is that you need to double click on the checkbox to check / uncheck the checkbox, unless I uninstall INotifyPropertyChanged. However, if he uninstalls it, the UI won't notice if the underlying data changes.
If I remove the Mode = TwoWay bit from the IsChecked binding expression, then also the UI won't notice the underlying data change, even if the model implements the interface.
How to do:
- Check the box data bound on startup
- Check the Fixed box to change the underlying data.
- Check the box to define baseline changes and data updates?
source to share