繁体   English   中英

如何对标签进行数据绑定

[英]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}"/>
....

在您的WindowLoadDataContext设置为Label

label.DataContext = class1ob.class2ob;//instance of class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM