1

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?

Yael
  • 1,566
  • 3
  • 18
  • 25
Leael
  • 11
  • 1
  • http://stackoverflow.com/questions/3562738/notify-one-object-when-a-property-of-another-object-changes – Robert Harvey Aug 31 '10 at 14:43
  • Thanks for reply, I know for PropertyChanged event, but I don't know how to use it within Entity Framework generated class...because there is already implementation of INotifyPropertyChanged on EntityObject class (ReportPropertyChanged event), but I can't use is, it trows an error... – Leael Sep 01 '10 at 07:21

2 Answers2

2

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
  • 5,539
  • 2
  • 21
  • 35
  • 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 Sep 01 '10 at 10:33
  • True, use just OnPropertyChanged then, the user interface will react just fine. I'll edit the answer. – Alex Paven Sep 01 '10 at 13:15
0

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 Hafalir
  • 1,752
  • 1
  • 15
  • 23
  • 1
    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 Aug 31 '10 at 14:37