[英]How to data-bind a label
How to bind the content of a label to the class2 property PropName
? 如何将标签的内容绑定到class2属性
PropName
?
Class2 is not directly being used in Mainwindlow.xmal.cs
. Mainwindlow.xmal.cs
没有直接使用Class2。
Class1
is being used in Mainwindow.xmal.cs
Class1
正在被使用Mainwindow.xmal.cs
And Class2
is being used in Class1
. 并且
Class2
正在Class1
。
Here is the code I'm using: 这是我正在使用的代码:
class Class2:INotifyPropertyChanged
{
string _PropName;
public string PropName
{
get
{
return this._PropName;
}
set
{
this._PropName = value;
OnPropertyChanged("PropName");
}
}
private void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(p));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class MainWindow : Window,INotifyPropertyChanged
{
Class1 class1ob;
public MainWindow()
{
InitializeComponent();
class1ob = new Class1();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
class1ob.changeProp();
}
}
I want to bind the content of a label eith the Class2
property - PropName
. 我想eith的标签的内容绑定
Class2
财产- PropName
。
How can I do that? 我怎样才能做到这一点?
Try this. 尝试这个。 XAML
XAML
....
<Label Name="label" Content="{Binding Path=PropName}"/>
....
On your WindowLoad
set DataContext
for Label
. 在您的
WindowLoad
将DataContext
设置为Label
。
label.DataContext = class1ob.class2ob;//instance of class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.