views:

229

answers:

2

Hello, I have a complex WPF control that for some reasons (ie. performance) is not using dependency properties but simple C# properties (at least at the top level these are exposed as properties).

The goal is to make it possible to bind to some of those top level properties -> I guess I should declare them as DPs.(right? or is there some other way to achieve this? )

I started reading on MSDN about DependencyProperties and DependencyObjects and found an example:

public class MyStateControl : ButtonBase
{
  public MyStateControl() : base() { }
  public Boolean State
  {
    get { return (Boolean)this.GetValue(StateProperty); }
    set { this.SetValue(StateProperty, value); } 
  }
  public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
    "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
}

If I'm right - this code enforces the property to be backed up by DependencyProperty which restricts it to be a simple property with a store(from functional point of view, not technically) instead of being able to calculate the property value each time getter is called and setting other properties/fields each time setter is called.

What can I do about that? Is there any way I could make those two worlds meet at some point?

//edit I guess I have to tell you a little more about what I want to do and what my limitations are. So:

I have TabControl that is bound to a collection of ViewModel(I'm using MVVM pattern) objects. Every tab is meant to be an editor for one object of that collection. Objects can be of different types so I have multiple definitions each with a different DataType property. Now I have that complex WPF Control that I want to use as a part of one of those DataTemplates. If I use usual TextBox I can simply bind to its Text property, but I can't do the same with Text property of that custom control simply because its Text property is not a dependency property. In this scenario I have :

  • no direct access to the control itself nor to its events
  • no code behind file that I can use to do that kind of thing

I can see however a dirty solution -

  1. In the Window class I would have to subscribe to CollectionChanged event of the collection that is bound to the TabControl.
  2. Whenever an item is added to that collection use ItemContainerGenerator to obtain a copy of I suppose TabItem and use it to find the right copy of 'complex control'
  3. Regiter items handlers to 'complex controls' events to do the job

This is wrong because:

  • this is agains MVVM - I have to play with tab control to do the job instead of doing it in the ViewModel class
  • this couples in an unwanted way the view and viewmodel
+1  A: 

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");
    }
}
Phil Sandler
Thanks for reply. I wrote some more info about my situation. (edited the post above)
kubal5003
A: 

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.

Robert Rossney
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