// Child View model
public class ChildViewModel
{
public ChildModel CurrentChild { get; set; }
private readonly List<ChildModel> _childList = new List<ChildModel>();
public ChildViewModel()
{
CurrentChild = new ChildModel();
}
public static ChildViewModel Create()
{
return ViewModelSource.Create<ChildViewModel>();
}
public void SaveMessage()
{
_childList.Add(ChildMessage);
}
}
//parent ViewModel public class ParentViewModel {
public ParentViewModel() {
Child = ChildViewModel.Create();
}
public virtual ChildViewModel Child {
get;
protected set;
}
}
//Parent View
fluentApi.BindCommand(simpleButton , x => x.Child.Save());
//Child View
fluentApi.SetObjectDataSourceBinding(ChildModelBindingSource, x => x.CurrentChild);
我在usercontrol中使用子视图模型中的对象CurrentChild绑定控件。 当我在父视图中单击simpleButton以在子视图模型中调用save方法但模型对象(CurrentChild)为null时(它不会更新它的vlaue)。 那么我怎样才能更新基于子视图模型的父视图模型属性。
答案 0 :(得分:0)
以下是适用于我的更正代码:
查看方:
var fluent = mvvmContext1.OfType<ParentViewModel>();
fluentApi.BindCommand(btnSave, x => x.Child.Save());
fluentApi.SetObjectDataSourceBinding(childModelbindingSource, x => x.Child.CurrentChild);
ViewModels方面:
// Child View model
public class ChildViewModel {
readonly List<ChildModel> _childList = new List<ChildModel>();
public ChildViewModel() {
CurrentChild = new ChildModel();
}
public static ChildViewModel Create() {
return ViewModelSource.Create<ChildViewModel>();
}
public ChildModel CurrentChild {
get;
private set;
}
public void Save() {
_childList.Add(CurrentChild);
}
}
// Parent ViewModel
public class ParentViewModel {
public ParentViewModel() {
Child = ChildViewModel.Create();
}
public virtual ChildViewModel Child {
get;
protected set;
}
}
public class ChildModel {
// ...
}
<强>更新强>
要使您的示例正常工作,您应该在初始化绑定之前将子ViewModel实例传递给ChildView :
// MainView
public MainView() {
InitializeComponent();
if(!mvvmContext1.IsDesignMode)
InitializeBindings();
}
void InitializeBindings() {
var fluent = mvvmContext1.OfType<MainViewModel>();
fluent.BindCommand(simpleButton1, x => x.ChildViewModel.Save());
var childView = new ChildView(fluent.ViewModel.ChildViewModel); // pass the ViewModel
this.panel1.Controls.Add(childView);
}
// Child View
public ChildView(ChildViewModel viewModel) {
InitializeComponent();
if(!mvvmContext1.IsDesignMode) {
mvvmContext1.SetViewModel(typeof(ChildViewModel), viewModel); // before any bindings
InitializeBindings();
}
}
void InitializeBindings() {
var fluent = mvvmContext1.OfType<ChildViewModel>();
fluent.SetObjectDataSourceBinding(childModelBindingSource, x => x.ChildData);
fluent.BindCommand(simpleButton1, x => x.Save());
}
主要说明:
当您遇到使用DevExpress产品的问题时,请contact the DevExpress support directly。不是Google,不是SO。
您可以向他们的人员提供您的问题的任何样本和所有其他详细信息,然后他们会为您提供有保证的正确解决方案。