|
|
3. Concepts Dependency Property
|
FAQ Home |
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.
|
public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterReadOnly(
|
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
|
3.2 How can I mark the default value of a custom dependency property to be false?
|
 |
public class MyStateControl : ButtonBase
|
public MyStateControl() : base() { }
|
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));
|
3.3 What are Attached Properties and how to register it?
|
 |
An "attached" property is a concept defined by Extensible Application Markup Language (XAML). It is intended to be used as a type of global property that can be set on any object. In Windows Presentation Foundation (WPF), attached properties are typically defined as specialized forms of dependency properties that do not have the conventional "wrapper" property.
This example shows how to 'register' an attached property and provide public accessors so that you can use the property in both Extensible Application Markup Language (XAML) and code. Attached properties are a syntax concept defined by Extensible Application Markup Language (XAML). Most of the attached properties for WPF types are also implemented as dependency properties. You can use dependency properties on any 'DependencyObject' types.
|
public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
|
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
|
public static void SetIsBubbleSource(UIElement element, Boolean value)
|
element.SetValue(IsBubbleSourceProperty, value);
|
public static Boolean GetIsBubbleSource(UIElement element)
|
return (Boolean)element.GetValue(IsBubbleSourceProperty);
|
3.4 How can I set an attached property in code?
|
 |
The following example shows how you can set an attached property in code.
|
DockPanel myDockPanel = new DockPanel();
|
CheckBox myCheckBox = new CheckBox();
|
myCheckBox.Content = "Hello";
|
myDockPanel.Children.Add(myCheckBox);
|
DockPanel.SetDock(myCheckBox, Dock.Top);
|
|
|
|
|