0
我有一個由屬性填充的文本框。當我點擊按鈕時,屬性值被改變,文本框內容也應該改變。然而我的財產改變事件不起作用。我已經在這裏和其他來源尋找解決方案。我沒有找到任何可能的幫助。有人能幫我嗎?PropertyChanged不適用於綁定
代碼:
using System.ComponentModel;
namespace TestWPF {
public class Class1 : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private string test = "test";
public string TestProperty {
get { return test; }
set {
if (value != test)
test = value;
OnPropertyChanged("TestProperty");
}
}
}
}
<Window.DataContext>
<local:Class1/>
</Window.DataContext>
<Grid>
<TextBox Text="{Binding Path=TestProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" x:Name="textBox" HorizontalAlignment="Center" Margin="0,-100,0,0" Height="69" TextWrapping="Wrap" VerticalAlignment="Center" Width="255"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Width="75" Click="button_Click"/>
</Grid>
編輯:
private void button_Click(object sender, RoutedEventArgs e) {
cs1.TestProperty = "Test button";
}
'cs1'(我想它是'Class1'的一個實例)!='Window.DataContext';窗口綁定到另一個上下文 – ASh
感謝您的幫助 –