Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - How to Trigger Data Binding to Method

 
 
Old 10-17-2007   #1 (permalink)
Runar Jordahl


 
 

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



My System SpecsSystem Spec
Old 10-17-2007   #2 (permalink)
Laurent Bugnion, MVP


 
 

Re: How to Trigger Data Binding to Method

Hi,

Runar Jordahl wrote:
Quote:

> 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
My System SpecsSystem Spec
Old 10-17-2007   #3 (permalink)
David


 
 

Re: How to Trigger Data Binding to Method

Quote:
Quote:

>> 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/lib...tychanged.aspx
or google for more info.

David.

My System SpecsSystem Spec
Old 10-17-2007   #4 (permalink)
Runar Jordahl


 
 

Re: How to Trigger Data Binding to Method

> That because that's where the "magic" is: If you bind to a normal
Quote:

> 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

My System SpecsSystem Spec
Old 10-17-2007   #5 (permalink)
Runar Jordahl


 
 

Re: How to Trigger Data Binding to Method

> If you make your class with the FullName property implement
Quote:

> 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

My System SpecsSystem Spec
Old 10-17-2007   #6 (permalink)
Laurent Bugnion, MVP


 
 

Re: How to Trigger Data Binding to Method

Hi,

Runar Jordahl wrote:
Quote:
Quote:

>> 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.
Quote:

> 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.
Quote:

> 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
My System SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46