2011-10-10 69 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」);'那裏。 –