![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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. 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 Specs![]() |
| | #5 (permalink) |
| | 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. 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 Specs![]() |
| | #6 (permalink) |
| | 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. 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. Otherwise, using DependencyObject is easier. Quote: > I'll look into your other suggestions. > > Runar 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 Specs![]() |