21

I have a project, where I bind a checkbox's IsChecked property with a get/set in the codebehind. However, when the application loads, it doesn't update, for some reason. Intrigued, I stripped it down to its basics, like this:

//using statements
namespace NS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool _test;
        public bool Test
        {
            get { Console.WriteLine("Accessed!"); return _test; }
            set { Console.WriteLine("Changed!"); _test = value; }
        }
        public MainWindow()
        {
            InitializeComponent();
            Test = true;
        }
    }
}

XAML:

<Window x:Class="TheTestingProject_WPF_.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <Viewbox>
        <CheckBox IsChecked="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Viewbox>
</Grid>

And, lo and behold, when I set it to true, it did not update!

Anyone can come up with a fix, or explain why?

Thanks, it'd be appreciated.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
  • Read [introductory material](http://msdn.microsoft.com/en-us/library/ms752347.aspx). – H.B. Feb 19 '13 at 19:38
  • 4
    I don't think I deserve a downvote because I read another source and not MSDN... – It'sNotALie. Feb 19 '13 at 19:40
  • 3
    @ofstream I didn't downvote, but I suspect it's because this question doesn't show any research effort. This problem is extremely basic, and anyone who works with WPF's binding system knows that you need to implement `INotifyPropertyChanged` to make your properties notify the UI to reevaluate the binding when they get changed. Pretty much every single WPF tutorial that introduces bindings covers this concept. – Rachel Feb 19 '13 at 19:44
  • 16
    That's exactly like saying newbs deserve downvotes because they don't know how to use basic methods. It's lack of knowledge. WPF is quite new to me. – It'sNotALie. Feb 19 '13 at 19:46
  • 3
    @ofstream The downvote wasn't for your lack of knowledge, it was for your lack of research effort – Rachel Feb 19 '13 at 19:47
  • 1
    You could aslo say that the newbs deserve downvotes because lack of research effort. MSDN + MSDN blogs have probably the answers to about 25% of all questions posted here, easily. – It'sNotALie. Feb 19 '13 at 19:53
  • @ofstream: You are allowed to ask questions that are also covered elsewhere, but *only if it has not been covered here yet*. And as i said, this has been asked and thus covered way too many times before. – H.B. Feb 19 '13 at 20:02
  • @H.B. OK, I get it now. – It'sNotALie. Feb 19 '13 at 20:11

1 Answers1

36

In order to support data binding, your data object must implement INotifyPropertyChanged

Also, it's always a good idea to Separate Data from Presentation

public class ViewModel: INotifyPropertyChanged
{
    private bool _test;
    public bool Test
    {  get { return _test; }
       set
       {
           _test = value;
           NotifyPropertyChanged("Test");
       }
    }

    public PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
         if (PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

<Window x:Class="TheTestingProject_WPF_.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Viewbox>
        <CheckBox IsChecked="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Viewbox>
</Grid>

Code Behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel{Test = true};
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154