2

I'm building desktop application in .NET 4.0 using WPF with SQL Server Compact Edition. I'm using Entity Framework 6.0 Code First approach as my ORM. My POCO class is quite simple:

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return FirstName + " " + LastName; } }
}

I'm displaying FullName property in the UI using following code

<TextBox Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="{Binding FullName}" />

When I update one of the properties, FirstName or LastName, the FullName doesn't update.

At first, I thought it is because my POCO class doesn't implement INotifyPropertyChanged (I'm to lazy to write OnPropertyChanged method inside all my properties setter :)).

But in the other hand, when I've tried Multibinding... it worked in some magical way:

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="Employee.FirstName"/>
    <Binding Path="Employee.LastName"/>
</MultiBinding>

Could anybody please explain to me, what event is trigering the UI update and how can I add my little EventHandler to this event?

gringo_dave
  • 1,372
  • 17
  • 24
  • 1
    This should help to explain the magic: http://stackoverflow.com/questions/7767218/why-does-the-binding-update-without-implementing-inotifypropertychanged =D – Chris Dec 01 '13 at 22:44
  • Magic man, magic ... :) I thought that this was somehow connected to DynamicProxies generated by Entity Framework. – gringo_dave Dec 01 '13 at 23:04
  • You should just be able to call NotifyPropertyChanged for 'FullName' in your FirstName and LastName setters which should update the view for FullName. The multibind is fine to use as long as you don't have to use FullName in your viewmodel. The multi-bind works without needing notifyproperychange bc you are binding to the same property and changing that value in the view not the viewmodel. It's kind of works like a mirror, any motions you make in front of a mirror will reflect in the mirror as well. – TMan Dec 01 '13 at 23:45
  • Thanks for that comment, I'm aware of the `INotifyPropertyChanged` interface and now a I'm also aware about `PropertyDescriptor`. But I'm lazy and encapsulating all private fields with properties inside my POCO class and adding the `OnPropertyChanged` method in each one is a lot of work. And code doesn't look nice and clean after this. EF dynamicProxy should have an event similar to `PropertyChanged` event in `INotifyPropertyChanged`, to which I could hook up and update my computed properties. – gringo_dave Dec 02 '13 at 08:36

0 Answers0