【发布时间】:2014-07-06 03:11:49
【问题描述】:
我刚开始使用 WPF,我正在尝试设置局部变量和标签之间的绑定。基本上我想在局部变量更改时更新标签。我正在寻找解决方案,但他们都只是使用文本框作为源,而不仅仅是类变量,我什至不确定它是否以这种方式工作。所以这是我的代码。
public partial class MainWindow : Window
{
int idCounter;
public MainWindow()
{
InitializeComponent();
Binding b = new Binding();
b.Source = idCounter;
b.Mode = BindingMode.OneWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myLabel.SetBinding(Label.ContentProperty,b);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
idCounter++;
}
}
按钮确实有效,idCounter 改变了值,但它没有在标签中更新,所以我猜绑定是错误的。有人可以告诉我有什么问题吗?谢谢
【问题讨论】:
标签: c# wpf binding event-handling controls