Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set a default value for a dependency property of type derived from dependencyobject

Tags:

c#

wpf

xaml

I am new to WPF and this is my first post. I have created a class called 'Fruit' that descends from 'DependencyObject' and adds and extra property called 'Apple'. I have created a new custom control that includes a Dependency Property called 'MyFruit' of type 'Fruit'. My question is, how can i set the default value for the properties within 'MyFruit' object (i.e. the 'Apple' property? I would like to set this in XAML using the object.

public class Gauge : Control {     .     .     .      //---------------------------------------------------------------------     #region MyFruit Dependency Property      public Fruit MyFruit     {         get { return (Fruit)GetValue(MyFruitProperty); }         set { SetValue(MyFruitProperty, value); }     }      public static readonly DependencyProperty MyFruitProperty =         DependencyProperty.Register("MyFruit", typeof(Fruit), typeof(CircularGauge), null);      #endregion   }    //------------------------------------------------------------------------- #region Fruit class  public class Fruit : DependencyObject {     private int apple;      public int Apple     {         get { return apple; }         set { apple = value; }     }   }  #endregion 
like image 564
Michael Smits Avatar asked Jul 18 '11 07:07

Michael Smits


People also ask

How do I set default value on dependency property?

When you define a custom XAML dependency property, one of the things you do is specify the default value. You can do this by providing the default value directly: DependencyProperty. Register("MyProperty", propertyType, ownerType, new PropertyMetadata(defaultValue));

How do you define dependency property?

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.

What is custom dependency property in WPF?

Dependency properties are properties that are registered with the WPF property system through Register or RegisterReadOnly calls. The Register method returns a DependencyProperty instance that holds the registered name and characteristics of a dependency property.

What is dependency property in C#?

A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the built-in feature of providing notification when the property has changed, data binding and styling.


2 Answers

Instead of null in your dependency property metadata insert

new UIPropertyMetadata("YOUR DEFAULT VALUE GOES HERE") 

So now it becomes

public static readonly DependencyProperty MyFruitProperty =     DependencyProperty.Register("MyFruit", typeof(Fruit), typeof(CircularGauge), new UIPropertyMetadata("YOUR DEFAULT VALUE GOES HERE")); 
like image 85
Zahid Sattar Avatar answered Oct 04 '22 02:10

Zahid Sattar


You need to use PropertyMetaData like this:

class MyValidation {     public bool status     {         get { return (bool)GetValue(statusProperty); }         set { SetValue(statusProperty, value); }     }          public static readonly DependencyProperty statusProperty = DependencyProperty.Register("status", typeof(bool), typeof(MyValidation),new PropertyMetadata(false)); } 
like image 36
Yakoob Hammouri Avatar answered Oct 04 '22 04:10

Yakoob Hammouri