Syncfusion WPF FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

3. Concepts Dependency Property

FAQ Home
   3.1 How can I create Custom Read-Only Dependency Properties ?
   3.2 How can I set an attached property in code?
   3.3 How can I mark the default value of a custom dependency property to be false?



3.1 How can I create Custom Read-Only Dependency Properties ?


The typical reason for specifying a read-only dependency property is that these are the properties that is used to determine the state, but where the state is defined in a multitude of factors. A typical example for a read-only property is IsMouseHover

This example shows how to 'register' an attached property as read-only. You can use dependency properties on any 'DependencyObject' types.

[C#]

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterReadOnly(
"IsBubbleSource",
typeof(Boolean),
typeof(AquariumObject),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);




3.2 How can I set an attached property in code?


The following example shows how you can set an attached property in code. In this example, 'myCheckBox' is an instance of the CheckBox class.

[C#]

DockPanel myDockPanel = new DockPanel();
CheckBox myCheckBox = new CheckBox();
myCheckBox.Content = "Hello";
myDockPanel.Children.Add(myCheckBox);
DockPanel.SetDock(myCheckBox, Dock.Top);




3.3 How can I mark the default value of a custom dependency property to be false?


Here is an example :

[C#]

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));
}



© 2001-2008 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap