2011-10-10 71 views
0

我有一个Foo类:绑定Button.IsEnabled一个属性在TextBox的数据源

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

和我有一个TextBoxButton势必Foo XAML:

<TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" ... /> 
<Button IsEnabled="{Binding IsDirty}" ... /> 

一旦TextBox中的文本被更改(在KeyDown上更新),Foo.IsDirty变为true(直到单击保存按钮)。

现在,Button.IsEnabledFoo.IsDirty更改时没有更改。

我该如何更改Button上的绑定,以便它在Foo.IsDirty = true之后立即生效,反之亦然?

谢谢!

回答

1

你需要实现你的Foo类INotifyPropertyChanged的接口:

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

我担心这可能是问题。不幸的是,Foo类扩展了一个类,该类是我无法修改的框架的一部分(并且IsDirty是框架的一部分),所以如果该属性没有通知,我想我必须找出其他的东西。谢谢! –

+2

为什么不创建一个从你的基类和'INotifyPropertyChange'扩展的类,并且'Foo'继承了它? – Rachel

+0

如果您可以更新'Value'属性的setter的代码,那么您可以在'Value'属性的setter中引发'OnPropertyChanged(“IsDirty”);'那里。 –