共享具有嵌套属性的 Observable 集合的最佳实践
Best practice for sharing an Observable-collection with nested properties
- 我有一个在不同视图模型之间共享的可观察集合。
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; }
}
函数PerformCalculations()
将执行,但它不会更新可观察集合中的所有属性。我了解到您不能使用可观察集合 .
所以,这就是我目前正在做的事情。
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;
}
}
- 我的问题是:是否有更好的共享可观察集合的方法?
非常感谢。
如果您希望 GUI 在列表中实例的 属性 发生变化时更新,您应该在实例 class 中实现 INotifyPropertyChanged
,就像您所做的那样在你的 ViewModel
.
您还没有显示您的 ParamClass
长什么样,所以我使用了 Person
class。您可以执行与 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
就是这样一个例子。还有很多其他人。
- 我有一个在不同视图模型之间共享的可观察集合。
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; }
}
函数
PerformCalculations()
将执行,但它不会更新可观察集合中的所有属性。我了解到您不能使用可观察集合 .所以,这就是我目前正在做的事情。
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;
}
}
- 我的问题是:是否有更好的共享可观察集合的方法?
非常感谢。
如果您希望 GUI 在列表中实例的 属性 发生变化时更新,您应该在实例 class 中实现 INotifyPropertyChanged
,就像您所做的那样在你的 ViewModel
.
您还没有显示您的 ParamClass
长什么样,所以我使用了 Person
class。您可以执行与 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
就是这样一个例子。还有很多其他人。