Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Property dependent on another

Tags:

How can one register a dependency property whose value is calculated using the value of another dependency property?

Because the .NET property wrappers are bypassed by WPF at run-time, one should not include logic in the getters and setters. The solution to that is typically to use PropertyChangedCallbacks. But those are declared static.

For example, what is the proper way to accomplish this contrived task:

public bool TestBool {   get { return (bool)GetValue(TestBoolProperty); }   set    {      SetValue(TestBoolProperty, value);     TestDouble = ((value)?(100.0):(200.0)); // HERE IS THE DEPENDENCY   } } public static readonly DependencyProperty TestBoolProperty =   DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel));  public double TestDouble {   get { return ((double)GetValue(TestDoubleProperty)); }   set { SetValue(TestDoubleProperty, value); } } public static readonly DependencyProperty TestDoubleProperty =   DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel)); 

As long as the dependency is not circular, is there a proper means to accomplish this?

like image 941
Gregyski Avatar asked Sep 01 '09 20:09

Gregyski


People also ask

What is a dependency property?

A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class.

What is the biggest feature of dependency property?

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification. The motivation for adding such intelligence to properties is to enable rich functionality directly from declarative markup.

What is difference between dependency property and attached property?

Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.

How does dependency property work internally?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.


1 Answers

Hmmm... I think you'd better look at dependency properties value coercion. Here is an example with coercion:

public class ViewModel : DependencyObject {   public bool TestBool   {     get { return (bool)GetValue(TestBoolProperty); }     set { SetValue(TestBoolProperty, value); }   }   public static readonly DependencyProperty TestBoolProperty =     DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel), new PropertyMetadata(false, OnTestBoolPropertyChanged));    private static void OnTestBoolPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)   {     var vm = (ViewModel)d;     vm.CoerceValue(TestDoubleProperty);   }    public double TestDouble   {     get { return ((double)GetValue(TestDoubleProperty)); }     set { SetValue(TestDoubleProperty, value); }   }   public static readonly DependencyProperty TestDoubleProperty =     DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel), new PropertyMetadata(0.0, null, OnCoerceTestDouble));    private static object OnCoerceTestDouble(DependencyObject d, object baseValue)   {     var vm = (ViewModel) d;     var testBool = vm.TestBool;     return ((testBool) ? (100.0) : (200.0));   } } 
like image 162
Anvaka Avatar answered Oct 05 '22 22:10

Anvaka