0

I've ToggleButton defined like this in XAML:

<ToggleButton IsChecked="{Binding DateFilter, ElementName=myUserControl, Mode=TwoWay}"/>

and 'DateFilter' defined like this:

public Boolean DateFilter { get; set; }

When I click the toggle-button, 'DateFilter' updates accordingly. BUT, if I modify 'DateFilter' in code, the ToggleButton doesn't update!
How can I do that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Fred
  • 596
  • 1
  • 6
  • 19

2 Answers2

0

You need to add inheritance from INotifyPropertyChanged for myUserControl and send in DateFilter setter event PropertyChanged with "DateFilter" property name.

Rover
  • 2,203
  • 3
  • 24
  • 44
0
public MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate {};

    private Boolean _dateFilter;

    public Boolean DateFilter
    {
        get { return _dateFilter; }
        set
        {
            _dateFilter = value;
            PropertyChanged(this, new PropertyChangedEventArgs("DateFilter");
        }
    }
}

Basically make that PropertyChanged call whenever you change the _dateFilter, or just use the setter, and you'll be sorted. Setting the event handler to an empty delegate allows you to avoid null checks.

Lunivore
  • 17,277
  • 4
  • 47
  • 92
  • Put it in a Resharper template - here's one: http://koder.wordpress.com/2010/03/25/resharper-inotifypropertychanged/ – Lunivore Sep 21 '10 at 23:44