【问题标题】:How do i make a UIElement change when a propertie change?属性更改时如何更改 UI 元素?
【发布时间】:2020-01-30 15:01:43
【问题描述】:

我正在尝试制作一个非常简单的应用程序来学习 DataBinding 和事件。以下代码应该在我单击按钮时更改标签内容,但实际上它会更改属性但不会更新标签。

这是主要代码:

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"; 
        }
    }

这是 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
}

有什么问题?当我更新Env.Name 时,不是假设更新label.content 吗?

【问题讨论】:

  • “不是假设更新 label.content” - 不。为什么要这样做?
  • @ASh omg 我对数据绑定什么都不懂,我可能真的很笨
  • 您没有任何数据绑定。这就是问题

标签: c# wpf events label bind


【解决方案1】:

您尚未将LabelContent 属性绑定到Name 属性。您刚刚将其设置为string。试试这个:

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

或者创建一个返回environmentsEnvironments 属性,将DataContext 设置为this 并绑定到Environments[index].Name。如果您不指定绑定的显式Source,它将在其当前DataContext 中查找可能从父元素继承的属性。请参阅docs 了解更多信息。

【讨论】:

  • 最好设置label.DataContext = e,而不是Binding.Source = e。或者最好的:使用 ItemsControl
  • @ASh:为什么会“更好”?
  • 谢谢,我不确定我是否了解有关 DataContext 的所有内容,但它应该对我有所帮助
  • @Axel,别担心,不需要理解。成功程序的道路是由 StackOverflow 用户为您编写代码铺平的。继续问
猜你喜欢
  • 2015-04-06
  • 2014-07-27
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2011-05-03
  • 2015-04-25
相关资源
最近更新 更多