我有一个包含集合的Model类和一个ViewModel封装类,该封装类实现了INotifyPropertyChanged
接口,并为模型类中的每个属性提供了一个封装属性。我这样实现是为了使模型类尽可能独立于任何WPF命名空间,因为这些类也用于另一个项目(Windows服务)。简化后的实现如下:
Model
public class FbiDirectory
{
private string type;
private ObservableCollection<PluginValue> pluginValues = new ObservableCollection<PluginValue>();
public string Type
{
get
{
return this.type;
}
set
{
this.type = value;
}
}
public ObservableCollection<PluginValue> PluginValues
{
get
{
return this.pluginValues;
}
set
{
this.pluginValues = value;
}
}
}
ViewModel 包装器
public class FbiDirectoryViewModel : INotifyPropertyChanged
{
private FbiDirectory fbiDirectory = new FbiDirectory();
public string Type
{
get
{
return this.fbiDirectory.Type;
}
set
{
this.fbiDirectory.Type = value;
this.OnPropertyChanged("Type");
this.OnPropertyChanged("Title");
}
}
public ObservableCollection<PluginValue> PluginValues
{
get
{
return this.fbiDirectory.PluginValues;
}
set
{
this.fbiDirectory.PluginValues = value;
this.OnPropertyChanged("PluginValues");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
我的问题是是否有一种方法可以将模型中的
PluginValues
集合类型更改为 List
,并且仍然具有ViewModel中 ObservableCollection
的功能。也许使用某种转换器或强制转换之类的方式可以实现这一点。
System.Collections.ObjectModel
和程序集System.dll
中。它不是WPF,而是核心.NET Framework。 - Clemens