问题
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