简体   繁体   English

属性更改时如何更改 UIElement?

[英]How do i make a UIElement change when a propertie change?

I am trying to make a really simple app to learn DataBinding and events.我正在尝试制作一个非常简单的应用程序来学习数据绑定和事件。 The following code is supposed to change the label content when i click on a button, but actually it changes the property but doesn't update the label.以下代码应该在我单击按钮时更改标签内容,但实际上它更改了属性但不更新标签。

This is the main code :这是主要代码:

public MainWindow()
    {
        InitializeComponent();

        environments = new ObservableCollection<Env>();

        environments.Add(new Env("env1", new ObservableCollection<Cell>()));
        environments.Add(new Env("env2", new ObservableCollection<Cell>()));

        foreach (Env e in environments)
        {
            Label label = new Label
            {
                Content = e.Name
            };

            pnlMain.Children.Add(label);  
        }
    }

    private void ChangeEnvName_Click(object sender, RoutedEventArgs e)
    {
        foreach (Env env in environments)
        {
            env.Name = "test"; 
        }
    }

And this is the Env class :这是 Env 类:

class Env : INotifyPropertyChanged
{
    //membres
    #region membres
    private string _name;
    private ObservableCollection<Cell> _cells;


    #endregion

    //propriétés
    #region propriétés
    public string Name
    {
        get { return this._name; }
        set
        {
            if (this._name != value)
            {
                this._name = value;
                this.NotifyPropertyChanged("Name");
            }
        }
    }
    public ObservableCollection<Cell> Cells
    {
        get { return this._cells; }
        set
        {
            if (this._cells != value)
            {
                this._cells = value;
                this.NotifyPropertyChanged("Cells");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion


    //méthodes
    #region méthodes
    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    #endregion

    //constructeur
    #region contructeur
    public Env(string name, ObservableCollection<Cell> cells)
    {
        _name = name;
        _cells = cells;
    }
    #endregion
}

What's the problem?有什么问题? Isn't it suppose the update the label.content when i update Env.Name ?是不是想更新的label.content当我更新Env.Name

You haven't bound the Content property of the Label to the Name property.您尚未将LabelContent属性绑定到Name属性。 You have just set it to a string .您刚刚将其设置为string Try this:尝试这个:

foreach (Env e in environments)
{
    Label label = new Label();
    label.SetBinding(Label.ContentProperty, new Binding("Name") { Source = e });
    pnlMain.Children.Add(label);
}

Or create an Environments property that returns environments , set the DataContext to this and bind to Environments[index].Name .或者创建一个返回environmentsEnvironments属性,将DataContext设置this并绑定到Environments[index].Name If you don specify an explicit Source of the binding, it will look for the property in its current DataContext which may be inherited from a parent element.如果您不指定绑定的显式Source ,它将在其当前DataContext查找可能从父元素继承的属性。 Please see the docs for more information.请参阅文档以获取更多信息。

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

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