|
|
|
date: Wed, 17 Oct 2007 08:58:26 +0200,
group: microsoft.public.windows.developer.winfx.avalon
back
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
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
|
|