mvvm计算字段 [英] mvvm calculated fields

查看:100
本文介绍了mvvm计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MVVM light工具包用于与wcf服务器通信的wpf应用程序.

I am using the MVVM light toolkit for a wpf application which talks to a wcf server.

wcf服务器返回一个Person对象(代理对象). 此人员对象具有多个字段,例如名称,姓氏等. 我的视图模型调用了Web服务,然后返回了该模型. 我的视图绑定到viewmodel的模型,并且字段正确绑定到每个ui文本框.

The wcf server returns a Person object (proxy object). This person object has several fields, name, surname, etc etc. My viewmodel calls the webservice, and then gets a return of this model. My view is bound to viewmodel's model, and the fields correctly bound to each ui textbox.

阴凉处,系统运行正常.

all cool in the shade, system functions nicely.

模型上的两个字段是DateOfBirth和NationalIDNumber (仅供参考:在南非,您可以从ID编号得出人的出生日期)

two fields on the model are DateOfBirth, and NationalIDNumber (fyi: in south africa you can derive a persons date of birth from an ID number)

因此,在用户输入或更新NationalIdNumber(如果可用)之后,我也希望确定DOB.

So after the user inputs or updtes the NationalIdNumber (if available) I would like the DOB to be determined as well.

但是DOB必须仍然映射到WCF服务返回的初始字段,因此我不能仅使用转换器将其绑定到NationalIdNumber.它需要与wcf代理的DOB字段保持绑定,以便可以持久保存.

But the DOB must still be mapped to the initial field that was returned from the WCF service, so I cant just bind it to the NationalIdNumber with a converter. It needs to stay bound to DOB field of the wcf proxy so that it can get persisted back.

我应该如何最好地实现这一目标?

how best should i implement this?

如果这是一个非mvvm项目,我将在IDNumber文本字段上放置一个事件,以便如果它失去焦点,请尝试从中计算一个dob(如果其中的文本是垃圾,可能并不总是可能的),然后覆盖Dob文本框的值.

If this was a non mvvm project, i would just put a event on the IDNumber text fields so that if it looses focus, try calculate a dob from it (might not always be possible if text in it is rubbish) and then overwrite the value of the Dob textbox.

我当时只是想调整Person对象的NationalIdNumber设置程序,但这在我更新Web服务引用的那一刻就会被删除

I was thinking of just tweaking the Person objects NationalIdNumber setter, but this will get removed the minute I update the webservice reference

谢谢

推荐答案

您可以在视图模型中具有Person属性:

You can have Person property in your view model:

ViewModel:

public class PersonViewModel : INotifyPropertyChanged
{
    Person person = new Person();
    public Person Person
    {
        get
        {
            return person;
        }
        set
        {
            person = value;
            NotifyPropertyChanged("Person");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

视图:

<TextBox Text="{Binding Person.NationalIDNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                 Height="23" HorizontalAlignment="Left" Margin="128,98,0,0" 
                 Name="textBox1" VerticalAlignment="Top" Width="120" />

因此,每当您更新Person的属性时,都会调用Person的setter.

So whenever you update Person's properties, Person's setter will be called.

...

使用MvvmLight:

Using MvvmLight:

ViewModel:

public class PersonViewModel : ViewModelBase
{
    Person person = new Person();
    public Person Person
    {
        get
        {
            return person;
        }
        set
        {
            person = value;
            RaisePropertyChanged("Person");
        }
    }
}

视图:

<TextBox Text="{Binding Person.NationalIDNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                 Height="23" HorizontalAlignment="Left" Margin="128,98,0,0" 
                 Name="textBox1" VerticalAlignment="Top" Width="120" />

这篇关于mvvm计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆