Several answers to questions about check box binding, for example this one suggest that
<checkbox IsChecked="{Binding Path=MyProperty, Mode=TwoWay}"/>
should work. What have I missed in the following code, which does not work?
<Window x:Class="TestBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<CheckBox Name="checkBox1" IsChecked="{Binding Path=MyProperty, Mode=TwoWay}">CheckBox</CheckBox>
</Grid>
</Window>
namespace TestBinding
{
public partial class Window1 : Window
{
bool property = true;
public Window1()
{
InitializeComponent();
}
public bool MyProperty
{
get
{
return property;
}
set
{
property = value;
}
}
}
}
update
3 good answers, many thanks, I’d like to accept them all, but have accepted the first that mentioned DataContext which was the part that affected the binding direction I was most interested in. The TwoWay mode has turned out to be a red herring.
And thanks for the MVVM suggestions, I am using it but wanted this code to be as simple as possible
Implement INotifyPropertyChanged, and set the DataContext to “this” :