Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何从聚合视图模型更新模型中的字符串属性_Wpf_Mvvm_Model_Viewmodel_Composition - Fatal编程技术网

Wpf 如何从聚合视图模型更新模型中的字符串属性

Wpf 如何从聚合视图模型更新模型中的字符串属性,wpf,mvvm,model,viewmodel,composition,Wpf,Mvvm,Model,Viewmodel,Composition,我正在WPF/MVVM中开发GUI应用程序。假设我有一个反序列化(第三方)XML文件的模型类 班级人员 { 公共字符串名称{get;set;} 公共字符串地址{get;set;} 公共字符串保留ID{get;set;} } 以及一个ViewModel,它向其视图公开许多属性和命令来操作模型,在本例中为字符串(如ReservationId): 类StringManipulator视图模型 { string modelString;//问题出在这里 公共StringManipulator视图模型(

我正在WPF/MVVM中开发GUI应用程序。假设我有一个反序列化(第三方)XML文件的模型类

班级人员
{
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串保留ID{get;set;}
}
以及一个ViewModel,它向其视图公开许多属性和命令来操作模型,在本例中为字符串(如ReservationId):

类StringManipulator视图模型
{
string modelString;//问题出在这里
公共StringManipulator视图模型(字符串模型字符串)
{
this.modelString=modelString;
}
//填充以让视图操纵字符串
}
StringManufactorViewModel具有高度的可重用性,许多ViewModels都使用它,例如

类PersonViewModel
{
人物模型;
public StringManufactorViewModel ReservationManufactorVM;//聚合ViewModel
public StringManufactorViewModel AddressManufactorVM;//聚合的ViewModel
公共PersonViewModel(个人模型)
{
this.model=模型;
ReservationManufactorVM=new StringManufactorViewModel(model.ReservationId);//问题出在这里
AddressManufactorVM=new StringManufactorViewModel(model.Address);//问题出在这里
}
}
显然,将字符串作为“model”传递给ViewModel是无效的,而且C#似乎不允许字符串引用作为字段。 在处理字符串类型时,让成员ViewModels操纵模型的最佳/正确方法是什么

谢谢

我想不出一种“正确”的方法来操作字符串作为
StringManufactorViewModel
中的引用,一旦将字符串作为值传递,它与模型无关

但是,只要
stringmanufactorviewmodel
模型
字符串值进行操作,就可以合法地更改
模型
字符串值,方法是在视图模型操纵字符串时引发
事件
,然后添加事件处理程序以使用新值更新模型:

类StringManipulator视图模型
{
字符串模型字符串;
公共事件事件处理程序;
公共StringManipulator视图模型(字符串模型字符串)
{
this.modelString=modelString;
}
公共开支()
{
//操纵字符串
StringOperated?.Invoke(这个,modelString);
}
}
PersonViewModel
构造函数中:

类PersonViewModel
{
人物模型;
公共StringManufactorViewModelReservationManufactorVM;
公共StringManufactorViewModelAddressManufactorVM;
公共PersonViewModel(个人模型)
{
this.model=模型;
ReservationManufactorVM=新的StringManufactorViewModel(model.ReservationId);
AddressManufactorVM=新的StringManufactorViewModel(model.Address);
ReservationManipulatorVM.StringManipulated+=(发送方,e)=>model.ReservationId=e;
AddressManipulatorVM.StringOperated+=(发送方,e)=>model.Address=e;
}
}

您的问题是您试图引用的是属性,而不是字符串字段

但是您可以将委托传递给属性的setter

如果还将modelString字段更改为属性,则可以在更改字符串时自动调用此委托

class StringManipulatorViewModel
{
    private string modelString
    {
        get { return _modelString; }
        set { _modelString = value; if (SetModelString != null) SetModelString(value); }
    }
    private string _modelString;
    Action<string> SetModelString;

    public StringManipulatorViewModel(string initialValue, Action<string> setModelString)
    {
        this.modelString = initialValue;
        SetModelString = setModelString;
    }
    //Stuff to let the view manipulate the string
}
当您想要传递属性时,这里有一些其他的想法


谢谢。在将模型更新逻辑保留在StringManipulator viewmodel中时,这种方法非常有吸引力。尽管功能非常强大,但这种方法将模型更新逻辑转移到聚合viewmodel之外,这对于我来说有点问题。无论如何,我已经看到我的代码的某些部分,您的方法将非常有用。非常感谢。
        ReservationManipulatorVM = new StringManipulatorViewModel(model.ReservationId, value => model.ReservationId = value); //here's the problem