WPF Exposing a calculated property for binding (as DependencyProperty)

Posted by kubal5003 on Stack Overflow See other posts from Stack Overflow or by kubal5003
Published on 2010-06-05T22:27:18Z Indexed on 2010/06/05 22:32 UTC
Read the original article Hit count: 572

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about dependency-properties