开发者

wpf twoway binding and update from code-behind

开发者 https://www.devze.com 2023-01-16 20:10 出处:网络
I\'ve ToggleButton defined like this in XAML: <ToggleButton IsChecked=\"{Binding DateFi开发者_如何学Golter, ElementName=myUserControl, Mode=TwoWay}\"/>

I've ToggleButton defined like this in XAML:

<ToggleButton IsChecked="{Binding DateFi开发者_如何学Golter, 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消