WPF Exposing a calculated property for binding (as DependencyProperty)?

It seems to me that you've been saddled with a WPF user control that was built by someone who didn't intend it to be used with data binding. I would assume that this is for one of two reasons: a) there's some logical reason that you shouldn't be able to bind to this property, or b) the original author of this control didn't know what he was doing. You haven't provided enough information for me to know which of those two conditions is the one you're really working under But in general, the reason you can't expose calculated properties for binding is that calculated properties generally don't have a setter.It doesn't make sense to set a property whose value is calculated If there are other properties whose values need to be updated when this one changes, the right approach (or at least the one consonant with the design of dependency properties) is to handle those updates in the dependency property's callback function, which is kind of what the callback function is for.

It seems to me that you've been saddled with a WPF user control that was built by someone who didn't intend it to be used with data binding. I would assume that this is for one of two reasons: a) there's some logical reason that you shouldn't be able to bind to this property, or b) the original author of this control didn't know what he was doing. You haven't provided enough information for me to know which of those two conditions is the one you're really working under.

But in general, the reason you can't expose calculated properties for binding is that calculated properties generally don't have a setter. It doesn't make sense to set a property whose value is calculated. If there are other properties whose values need to be updated when this one changes, the right approach (or at least the one consonant with the design of dependency properties) is to handle those updates in the dependency property's callback function, which is kind of what the callback function is for.

You are perfectly right. The property that I was trying to bind to wasn't meant for that purpose. I found another property that essentially does the same and it is a DP and I can bind to it.

Unfortunately the binding isn't working at all, but that's another day spent on debugging and another story.. (I'll probably ask about that too but not here) – kubal5003 Jun 6 '10 at 21:38.

I think you are mixing up Dependency Properties and implementing INotifyPropertyChanged on your classes. You don't need your property it to be a dependency property, you just need your class to implement INotifyPropertyChanged and call OnPropertyChanged whenever the state of your object changes in a way that would affect the value you want to expose to binding. So let's say you have a property "Sum" that you want to bind to.

The Sum property simple addes two other properties (or fields, or whatever) together. When anything happens that affects the Sum calculation, you want to notify that the Sum value has changed, so the any controls bound to Sum get updated. Public int Sum { get{ return Value1 + Value2 }; } public int Value1 { set { //changing this affects "Sum", so I need to notify that the binding should update _value1 = value; OnPropertyChanged("Sum"); } } public int Value2 { set { //Changing this affects "Sum", so I need to notify that the binding should update _value2 = value; OnPropertyChanged("Sum"); } }.

Thanks for reply. I wrote some more info about my situation. (edited the post above) – kubal5003 Jun 6 '10 at 10:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions