mvvm calculated fields

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 08:06:09

问题


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

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.

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)

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

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?

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.

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

Thanks


回答1:


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));
        }
    }
}

View:

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

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

...

Edit:

Using MvvmLight:

ViewModel:

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

View:

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



回答2:


  public class PropertyHelpersViewModel : INotifyPropertyChanged
    {
        private string text;
        public string Text
        {
            get { return text; }
            set
            {
                if(text != value)
                {
                    text = value;
                    RaisePropertyChanged("Text");
                }
            }
        }

        protected void RaisePropertyChanged(string propertyName)
        {
            var handlers = PropertyChanged;
            if(handlers != null)
                handlers(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }


来源:https://stackoverflow.com/questions/14237180/mvvm-calculated-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!