edd*_*die 9 c# design-patterns mvvm winforms
我正在尝试构建一个简单的C#/ WinFoms项目,该项目根据结构使用Model-View-ViewModel设计模式:
两个UserControl和关联的ViewModel之间的数据绑定不能很好地工作.
将MainForm包含两个用户控件(UC): ,.Uc_Create Uc_Iteration每个UC都包含一个组合框,它连接到ViewModel_xxx中的相关属性,即
Uc_Create有:
this.comboBox1ComplexCreate.DataSource = oVM_Create.VM_Create_ListOfStringsInModel;
Run Code Online (Sandbox Code Playgroud)
Uc_Iteration有:
this.comboBox1ComplexIteration.DataSource = oVM_Iteration.VM_Iteration_ListOfStringsInModel;
Run Code Online (Sandbox Code Playgroud)
问题:
当我VM_Iteration_ListOfStringsInModel在相应的UC(comboBox1ComplexCreate)中向组合框添加元素并且模型中的列表被正确更改但其他组合框(comboBox1ComplexIteration)中Uc_Iteration没有!
为什么????
如果我List将模型更改为a BindingList,一切正常.我究竟做错了什么?
提前致谢!
模型:
namespace Small_MVVM
{
public class Model
{
private static readonly object m_oLock = new object();
private static Model instance;
public List<string> simplelistOfStrings;
private Model()
{
simplelistOfStrings = new List<string>();
}
public static Model GetInstance()
{
if (instance == null)
{
lock (m_oLock)
{
if (instance == null)
{
instance = new Model();
}
}
}
return instance;
}
}
}
Run Code Online (Sandbox Code Playgroud)
ModelView_Create:
namespace Small_MVVM
{
class ViewModel_Create : NotifyPropertyChangedBase
{
private static Model oModel = Model.GetInstance();
private BindingList<string> _VM_Create_ListOfStringsInModel = new BindingList<string>(oModel.simplelistOfStrings);
public BindingList<string> VM_Create_ListOfStringsInModel
{
get
{
return _VM_Create_ListOfStringsInModel;
}
set
{
_VM_Create_ListOfStringsInModel = value;
this.FirePropertyChanged(nameof(VM_Create_ListOfStringsInModel));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ModelView_Iteration:
namespace Small_MVVM
{
class ViewModel_Iteration : NotifyPropertyChangedBase
{
private static Model oModel = Model.GetInstance();
public ViewModel_Iteration()
{
}
BindingList<string> _VM_Iteration_ListOfStringsInModel = new BindingList<string>(oModel.simplelistOfStrings);
public BindingList<string> VM_Iteration_ListOfStringsInModel
{
get
{
return _VM_Iteration_ListOfStringsInModel;
}
set
{
_VM_Iteration_ListOfStringsInModel = value;
this.FirePropertyChanged(nameof(VM_Iteration_ListOfStringsInModel));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是NotifyPropertyChangedBase实现INotifyPropertyChange接口的抽象类:
public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool CheckPropertyChanged<T>(string propertyName, ref T oldValue, ref T newValue)
{
if (oldValue == null && newValue == null)
{
return false;
}
if ((oldValue == null && newValue != null) || !oldValue.Equals((T)newValue))
{
oldValue = newValue;
return true;
}
return false;
}
private delegate void PropertyChangedCallback(string propertyName);
protected void FirePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您遵循MVVM模式,那么您应该使用ObservableCollection如下所示的集合
private ObservableCollection<int> _intList;
public ObservableCollection<int> IntList
{
get
{
return _intList;
}
set
{
_intList= value;
_intList.CollectionChanged +=
new System.Collections.Specialized.NotifyCollectionChangedEventHandler
(MyProperty_CollectionChanged);
}
}
void MyProperty_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
NotifyPropertyChanged("IntList");
}
Run Code Online (Sandbox Code Playgroud)
ObservableCollection - 表示动态数据集合,在添加,删除项目或刷新整个列表时提供通知.
你也可以检查一下:使用C#的Windows窗体应用程序的MVVM(Model-View-ViewModel)模式