views:

20

answers:

2

Hi,

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?

Thx
Fred

A: 

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

Rover
A: 
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
Cool thx!Well, I though, it could be much simpler!
Fred
Put it in a Resharper template - here's one: http://koder.wordpress.com/2010/03/25/resharper-inotifypropertychanged/
Lunivore