ViewModelToModel

Topics: Questions
Feb 5, 2013 at 12:40 PM
Hello.

Want to map an Property from an Model to an ViewModel. But these Propertys of different types.

Model = Int
ViewModel = Enum

So is it possible to cast in the mapping?
Feb 6, 2013 at 8:21 AM
No, Casting is not supported.
Feb 11, 2013 at 10:27 AM
Ok. So what I have to do?

Implement 2 Propertys in the ViewModel. One with the same type like in the model. And one with the new type. But then its difficult to map the both propertys.
Feb 12, 2013 at 10:05 AM
You can override both the OnModelPropertyChanged and OnPropertyChanged in the view model. Then you know which model has changed which property and do the cast yourself.
Feb 12, 2013 at 1:55 PM
Now I did it in this way. And it works.
[Model]
public Unit Unit
{
    get { return GetValue<Unit>(UnitProperty); }
     private set { SetValue(UnitProperty, value); }
}
public OpMode OmUnit
{
    get { return (OpMode)Unit.OmUnit; }
    set
    {
        if ((OpMode) Unit.OmUnit != value)
        {
            Unit.OmUnit = (int)value;
            RaisePropertyChanged("OmUnit");
        }
     }
}
Feb 12, 2013 at 6:42 PM
That's a good option as well.