Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Exposing a calculated property for binding (as DependencyProperty)

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
like image 790
kubal5003 Avatar asked Jun 05 '10 22:06

kubal5003


People also ask

What is a dependency property in WPF?

Unlike a common language runtime (CLR) property, a dependency property adds support for styling, data binding, inheritance, animations, and default values. Background, Width, and Text are examples of existing dependency properties in WPF classes.

How to set the value of a dependency property?

When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set. The advantages of dependency properties are.

Why do we use XAML identifiers for dependency properties?

Not only does the property system uses the identifier, XAML processors may use it, and your code (and possibly external code) can access dependency properties through their identifiers. Dependency properties can only be applied to classes that are derived from DependencyObject types.

How do I assign a dependencyproperty to a static readonly field?

You'll assign the DependencyProperty instance to a static readonly field, known as a dependency property identifier, that by convention is named <property name>Property. For example, the identifier field for the Background property is always BackgroundProperty.


1 Answers

I think you are mixing up Dependency Properties and implementing INotifyPropertyChanged on your classes.

You don't need your property 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 adds 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 => 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");
    }
}
like image 104
Phil Sandler Avatar answered Sep 30 '22 14:09

Phil Sandler