Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Wed, 17 Oct 2007 08:58:26 +0200,    group: microsoft.public.windows.developer.winfx.avalon        back       


How to Trigger Data Binding to Method   
I have a class Person with two properties FirstName and LastName. I hook up 
two WPF GUIs (each having two text boxes) to the same Person instance using 
these data bindings Text="{Binding FirstName}" and Text="{Binding 
LastName}". This works great. I can edit the first or last name in one GUI 
and the corresponding text box in the other GUI is immediately updated.

In one way this came as a surprise to me. I expected that the property 
setters would somehow need to signal that the property had changed. But all 
they do is simply set the variable, like this example "firstName = value". I 
assume something magic happens at the CLR-level, but I have not found out 
about this.

Now on to my problem. Class Person also has the following method, which 
returns the person's full name:
public string FullName() {return firstName + " " + lastName; }

I hook this method up to a text block (using Text="{Binding 
Path=FullName}"), and as expected, this binding does not refresh when one of 
the GUIs change FirstName or LastName. I looked at bit at Events, but I 
cannot see how they can be used in this context to make the binding trigger.

Basically, I wonder how I can make the binding to FullName trigger when 
FirstName or FirstName trigger.

Runar
date: Wed, 17 Oct 2007 08:58:26 +0200   author:   Runar Jordahl

Re: How to Trigger Data Binding to Method   
Hi,

Runar Jordahl wrote:
> I have a class Person with two properties FirstName and LastName. I hook up 
> two WPF GUIs (each having two text boxes) to the same Person instance using 
> these data bindings Text="{Binding FirstName}" and Text="{Binding 
> LastName}". This works great. I can edit the first or last name in one GUI 
> and the corresponding text box in the other GUI is immediately updated.
> 
> In one way this came as a surprise to me. I expected that the property 
> setters would somehow need to signal that the property had changed. But all 
> they do is simply set the variable, like this example "firstName = value". I 
> assume something magic happens at the CLR-level, but I have not found out 
> about this.
> 
> Now on to my problem. Class Person also has the following method, which 
> returns the person's full name:
> public string FullName() {return firstName + " " + lastName; }
> 
> I hook this method up to a text block (using Text="{Binding 
> Path=FullName}"), and as expected, this binding does not refresh when one of 
> the GUIs change FirstName or LastName. I looked at bit at Events, but I 
> cannot see how they can be used in this context to make the binding trigger.
> 
> Basically, I wonder how I can make the binding to FullName trigger when 
> FirstName or FirstName trigger.
> 
> Runar

That because that's where the "magic" is: If you bind to a normal 
property, the UI won't be refreshed when the property changes. It needs 
to be a Dependency Property (DP). See MSDN to check how to register a DP 
with the framework so that it is watched.

Alternatively, you could do a multibinding to FirstName and LastName, 
with a converter to create the FullName string based on FirstName and 
LastName.

Or, you could use the EvalBinding extension created by IdentityMine 
(check their website for information.

HTH,
Laurent
-- 
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
date: Wed, 17 Oct 2007 07:02:00 -0700   author:   Laurent Bugnion, MVP

Re: How to Trigger Data Binding to Method   
>> Basically, I wonder how I can make the binding to FullName trigger when
>> FirstName or FirstName trigger.
>>
>> Runar
>


If you make your class with the FullName property implement 
INotifyPropertyChanged then the binding will be aware of changes to it. 
It's very simple to do.

See: 
http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx 
or google for more info.

David.
date: Wed, 17 Oct 2007 15:30:36 +0100   author:   David

Re: How to Trigger Data Binding to Method   
> That because that's where the "magic" is: If you bind to a normal
> property, the UI won't be refreshed when the property changes. It needs
> to be a Dependency Property (DP). See MSDN to check how to register a DP
> with the framework so that it is watched.

Thank you for your reply! Regarding using "Dependency Property":  I
thought these were mostly for components that are directly part of
WPF, and not domain objects. Adam Nathan's "Windows Presentation
Foundation Uneashed" states that all classes having dependency
properties must derive from System.Windows.DependencyObject (page 52).
My intent is to let Person be a simple domain object inheriting from
Object.

I'll look into your other suggestions.

Runar
date: Wed, 17 Oct 2007 19:49:12 -0000   author:   Runar Jordahl

Re: How to Trigger Data Binding to Method   
> If you make your class with the FullName property implement
> INotifyPropertyChanged then the binding will be aware of changes to it.
> It's very simple to do.

Thank you for that suggestion. I tried it and it works. In my setter
for FirstName I can do this:

firstName = value;
NotifyPropertyChanged("FirstName");
 NotifyPropertyChanged("FullName");

What surprised me was that by adding the interface
INotifyPropertyChanged the "magic" event triggering of properties are
gone. Therefore I now also need to notify the change to the property
itself (the second line in the example above).

Runar
date: Wed, 17 Oct 2007 20:15:47 -0000   author:   Runar Jordahl

Re: How to Trigger Data Binding to Method   
Hi,

Runar Jordahl wrote:
>> That because that's where the "magic" is: If you bind to a normal
>> property, the UI won't be refreshed when the property changes. It needs
>> to be a Dependency Property (DP). See MSDN to check how to register a DP
>> with the framework so that it is watched.
> 
> Thank you for your reply! Regarding using "Dependency Property":  I
> thought these were mostly for components that are directly part of
> WPF, and not domain objects.

Not at all. DependencyObjects are very handy also for data objects which 
you want to bind to. Unfortunately, if you want to make your existing 
classes DependencyObjects, it involves some rework, so it's not always 
possible. This is why the INotifyPropertyChanged also exists, but it 
involves raising the PropertyChanged events yourself, while it is 
automatic if you use the DependencyProperty infrastructure.

> Adam Nathan's "Windows Presentation
> Foundation Uneashed" states that all classes having dependency
> properties must derive from System.Windows.DependencyObject (page 52).
> My intent is to let Person be a simple domain object inheriting from
> Object.

If you cannot change this, then using INotifyPropertyChanged is OK. 
Otherwise, using DependencyObject is easier.

> I'll look into your other suggestions.
> 
> Runar

Greetings,
Laurent
-- 
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
date: Wed, 17 Oct 2007 17:24:57 -0700   author:   Laurent Bugnion, MVP

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us