【发布时间】:2016-05-11 11:12:25
【问题描述】:
我有点迷茫,我试图将我的 GUI 上的标签绑定到我的代码中的一个字符串,但它似乎只有在我将代码放在我的 MainWindow() 块中并且我无法更新时才有效它来自其他任何地方。
这是我的 INotifyPropertyChanged 类:
public class MyClass : INotifyPropertyChanged
{
private string _testLabel;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
public string testLabel
{
get { return _testLabel; }
set
{
if (value != _testLabel)
{
_testLabel = value;
OnPropertyChanged("testLabelChanged");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
这是 MainWindow() 块:
public MainWindow()
{
InitializeComponent();
testLabelName.DataContext = t;
t.testLabel = "Hello World 32432";
}
我也在外面声明过:
MyClass t = new MyClass();
这是来自我的 XAML 的 sn-p:
<Label Name="testLabelName" Content="{Binding Path=testLabel, Mode=OneWay}" Height="28" Margin="0,0,12,29" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="128" />
我尝试通过使用按钮事件设置标签对其进行测试,它似乎已设置,但 GUI 上没有任何变化。我认为 GUI 没有更新 PropertyChanged 事件。是这样吗?
谢谢,
【问题讨论】:
标签: c# wpf xaml binding propertychanged