开发者

Bind Button.IsEnabled to a property in a TextBox's data source

I have a Foo class:

public class Foo
{
    public string Value { get; set; }
    public string IsDirty { get; private set; }
}

and I have xaml with a TextBox and a Button bound to Foo:

<TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyC开发者_StackOverflow社区hanged}" ... />
<Button IsEnabled="{Binding IsDirty}" ... />

Once the text in the TextBox is changed (updated on KeyDown), Foo.IsDirty becomes true (until a save button is clicked).

Right now, Button.IsEnabled is not changing when Foo.IsDirty changes.

How might I change the binding on the Button so that it becomes enabled as soon as Foo.IsDirty = true and vice versa?

Thanks!


You need to implement the interface INotifyPropertyChanged in your Foo class:

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }


private bool _isDirty;

public bool IsDirty { get{ return _isDirty;}
                      private set{
                          _isDirty= value;
                          OnPropertyChanged("IsDirty"); }
                    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜