【问题标题】:Best practice for sharing an Observable-collection with nested properties与嵌套属性共享 Observable 集合的最佳实践
【发布时间】:2020-10-21 07:46:54
【问题描述】:
  1. 我有一个在不同视图模型之间共享的可观察集合。
public class UserInput1ViewModel: INotifyPropertyChanged
{
        public ObservableCollection<ParamClass> ParamColl { get; set; }

        public UserInput1ViewModel(<ParamClass> paramColl)
        {
            this.ParamColl = paramColl;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }       

        private void UpdateCollection()
        {            
            this.ParamList = PerformCalculations();

        }

}

public class ParamClass
{
        public double Property1 { get; set; }
        public double Property2 { get; set; }
        public double Property3 { get; set; }
        ...                   ...
        ...                   ...
        public double Property19 { get; set; }

}
  1. 函数PerformCalculations() 将执行,但它不会更新可观察集合中的所有属性。我了解到您无法使用可观察集合 https://stackoverflow.com/a/9984424/4387406 来做到这一点。

  2. 所以,这就是我目前正在做的事情。

        private void UpdateCollection()
        {            
            var output = PerformCalculations();
            for(int i = 0; i < output.Count(); i++)
            {
                this.ParamColl[i].Property1 = output[i].Property1;
                this.ParamColl[i].Property2 = output[i].Property2;
                       ...                   ...
                       ...                   ...
                this.ParamColl[i].Property19 = output[i].Property19;
            }
        }
  1. 我的问题是:有没有更好的方法来共享 observable 集合?

非常感谢。

【问题讨论】:

  • 为了使ParamColl[i].Property1 = output[i].Property1; 更新 UI,ParamClass 必须实现 INotifyPropertyChanged 接口。如果 PerformCalculations 返回一个 ParamClass 的集合,你当然可以写 ParamColl[i] = output[i];
  • 谢谢 Clemens,我认为 INotifyPropertyChanged 接口仅适用于视图模型。这对我来说是很棒的学习!谢谢!

标签: c# wpf collections viewmodel


【解决方案1】:

如果您希望在列表中实例的属性更改时更新 GUI,您应该在实例类中实现 INotifyPropertyChanged,就像您在 ViewModel 中所做的那样。

您尚未展示您的 ParamClass 的外观,因此我正在使用 Person 类。您可以像在 ViewModel 中所做的那样做同样的事情。

public class Person : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }

    private int age;
    public int Age
    {
        get { return age; }
        set { age = value; OnPropertyChanged("Age"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

现在,即使任何实例中的单个属性发生更改,它也会反映在您的 GUI 上。

由于您使用的是 WPF,因此有很多不错的 MVVM 工具包可以为您做很多事情。例如,MVVM Light Toolkit 就是这样一个例子。还有很多其他的。

【讨论】:

  • 嗨,萨赫,非常感谢您的回答。我不知道您也可以将 INotifyPropertyChanged 接口应用于实例类。我认为它仅适用于 viewModel 类。凉爽的!这太棒了。我只是在学习 MVVM 的工作原理(而且我知道我的程序正在执行一个糟糕的程序)。感谢您提供工具包的链接。我一定会学习使用 MVVM Light Toolkit。谢谢!!1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多