Search code examples
c#wpfeventslabelbind

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 :

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 ?


Solution

  • You haven't bound the Content property of the Label to the Name property. You have just set it to a 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. 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. Please see the docs for more information.