[英]How to data-bind a label
如何將標簽的內容綁定到class2屬性PropName
?
Mainwindlow.xmal.cs
沒有直接使用Class2。
Class1
正在被使用Mainwindow.xmal.cs
並且Class2
正在Class1
。
這是我正在使用的代碼:
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();
}
}
我想eith的標簽的內容綁定Class2
財產- PropName
。
我怎樣才能做到這一點?
嘗試這個。 XAML
....
<Label Name="label" Content="{Binding Path=PropName}"/>
....
在您的WindowLoad
將DataContext
設置為Label
。
label.DataContext = class1ob.class2ob;//instance of class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.