Click here to Skip to main content
15,887,347 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have attempted to bind to a custom class I have created (i.e., WpfFlags class), but have been unable to get the value to update with change, and was hoping somebody could help me. I know the binding is working because the PropertyChangedCallback is being executed when the view loads, but executing PropertyChanged(this, new PropertyChangedEventArgs("GraphicFlags")) is not causing the property to be updated. I know that the code is working because I did a workaround where I bind to the critical proptery in the class (int32), and then recreate an instance of the WpfFlags class in the PropertyChangedCallback for this DependencyProperty in View’s code behind . Then I change the value of the WpfFlags DependencyProperty in the code behind, which then does create the desired result.

I attempted to inherit from a DependencyObject for the WpfFlags class, but that had no effect. I also have investigated the TypeDescriptor, but I do not think that is the answer.

Help would be very much appreciated.
Posted
Comments
Mark Salsbery 24-Jun-11 18:51pm    
Is it a c# property in a class implementing INotifyPropertyChanged or a dependency property? You mention the property not getting updated through a binding...is that binding OneWay mode when it needs to be TwoWay?
Clifford Nelson 24-Jun-11 19:15pm    
The WpfFlags class is being used in the Binding, and the class that changes a property in the WpfFlags class is executing the PropertyChanged event. I implementing the INotifyPropertyChanged in the WpfFlags class, and that had no effect, and I made the Binding Mode TwoWay and that did not work. As I said, and maybe this does not matter, when I just pass an integer value using effectively the same code, everything works, but not when I try to pass the class in the binding. The only thing I can think of that I could attempt is the implement the INotifyPropertyChanged in the WPF class, but then I would be raising and event that the class itself has changed, and what name would I use in the string?
Mark Salsbery 24-Jun-11 19:32pm    
You bind to properties of a class, not to a class. If you expect changes to properties in your class to notify a binding that the property has changed then the class needs to implement INotifyPropertyChanged and make sure the PropertyChanged event is raised for the property when a change is made. That's it. Can you post a small example of the class with a property and a binding to that property that is failing?
Clifford Nelson 24-Jun-11 19:58pm    
As explained in the writeup, I want to bind to the class. Wanted to find out if there is a way that I can customize a class so I can bind to it. That way I can create something that abstracts and encapsulates functionality. I explained in the writeup that I was successful in binding to a property in the class, and then use it to regenerate the class, but that does not give me the abstraction i desire.
Mark Salsbery 25-Jun-11 0:02am    
You can't bind to a class. Bindings need a runtime object (instance) of a class. You can bind to an instance of your class by setting the appropriate DataContext to the instance and not specifying a path in the binding (e.g. {Binding}). That will bind to the entire object, not a specific property.

1 solution

This was a problem with the fact that the binding system did not understand that the object (class instance) changed. The way to fix this is to change the property to a totally new instance of the class and then raise the property changed event. unfortunately as far as I can tell there is no way to tell the Binding that a value within the class has changed and thus needs to recalculated binding values. Changing the value returned by GetHashCode did not help either.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900