我有一個.xaml文件和一個與Binding共享值的.cs文件。更新值的WPF綁定問題
爲了簡單起見,我有1個按鈕和1個文本框。當文本框的文本沒有字符時,我想要禁用按鈕。
這裏是XAML的兩個代碼綁定:
<TextBox Name="txtSend" Text="{Binding Path=CurrentText,UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Button IsEnabled="{Binding Path=IsTextValid}" Name="btnSend">Send</Button>
中的.cs兩個屬性文件看起來像:
public string CurrentText
{
get
{
return this.currentText;
}
set
{
this.currentText = value;
this.PropertyChange("CurrentText");
this.PropertyChange("IsTextValid");
}
}
public bool IsTextValid
{
get
{
return this.CurrentText.Length > 0;
}
}
的this.PropertyChanged
很簡單,就是調用PropertyChanged
的方法來自INotifyPropertyChanged。
問題是,我必須調用CurrentText的Setter中的this.PropertyChange("IsTextValid");
才能夠更改按鈕狀態。
問題1)這樣做的好方法...如果規則變得更復雜,我可能需要調用很多PropertyChanged ...?
問題2)我的按鈕在窗體加載時啓用。我怎樣才能使它從一開始就檢查方法?
+1和答案接受:) +25代表你的朋友。謝謝。 – 2009-07-11 17:34:46