views:

33

answers:

2

First, I apologize for my low level English writing.

I use Entity Framework and data-binding in WPF MVVM project. I would like to know what is the best way to do data binding to added calculated property of EntityObject which is generated with Entity Framework.

For example:

    partial class Person
    {
        partial string Name...

        partial string Surname...

        public string FullName
        {
            get { return Name + Surname; }
        }
    }

And then in XAML something like ...Text="{Binding FullName, Mode=Twoway}"

At this point my GUI doesn't know when property FullName is changed... how can I notify it? I've tried with ReportPropertyChanged but it return's an error...

Also, I wonder what is the best way to implement binding when one binding depends on more properties...calculated properties or value converters or something different?

Thanks a lot

A: 

I am not sure if you are looking for this kind of thing:

public string FullName
{
    get { return Name + Surname; }
    set 
    {
        // You should do some validation while and before splitting the value
        this.Name = value.Split(new []{' '})[0];
        this.Surname = value.Split(new []{' '})[1];
    }
}
Musa Hafalır
No, this solves the wrong problem. When somebody changes just the `Surname`, he needs a way to notify clients that the `FullName` has changed.
Gabe
That's right, Gabe
Leael
A: 

You could subscribe to the PropertyChanged event in the constructor and if the property name matches either of the two source properties, raise the event for the calculated one.

public Person()
{
    this.PropertyChanged += (o, e) =>
        {
            if (e.PropertyName == "Name" || e.PropertyName == "Surname") OnPropertyChanged("FullName");
        };
}
Alex Paven
Tnx for reply, I've done something similar, but it appears that I can't use ReportPropertyChanged on calculated property, i.e. property that is not mapped...it throws an error...
Leael
True, use just OnPropertyChanged then, the user interface will react just fine. I'll edit the answer.
Alex Paven