|
|
Questions and answers for Concepts Dependency Property.
|
3. Concepts Dependency Property
|
| 3.1 How can
I create Custom Read-Only Dependency Properties ?
|
| 3.2 How can
I use Dependency Property MetaData?
|
| 3.3 How can
I register the default value of the State 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. For example, the property IsMouseOver can be
used when the user hovers over some element, attempting to change this value
programmatically would cause inconsistency.
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 use Dependency Property MetaData?
|
|
Dependency property metadata exists as an object that can be queried to examine
the characteristics of a dependency property. This metadata is also accessed
frequently by the property system as it processes any given dependency
property.
The metadata object for a dependency property can contain the following types
of information.
-
Default value for the dependency property, if no other value can be determined
for the dependency property by local value, style, inheritance, etc. For a
thorough discussion of how default values participate in the precedence used by
the property system when assigning values for dependency properties, see
'Dependency Property Value Precedence'.
-
References to callback implementations that affect coercion or change
notification behaviors on a per-owner-type basis. Note that these callbacks are
often defined with a non-public access level. So obtaining the actual
references from metadata is generally not possible, unless the references are
within your permitted access scope. For more information on dependency property
callbacks, see 'Dependency Property Callbacks and Validation'.
-
If the dependency property in question is considered to be a WPF framework
level property, the metadata might contain WPF framework level dependency
property characteristics, which report information and state for services such
as the WPF framework level layout engine and property inheritance logic.
|
3.3 How can I register the default value of the State dependency property to be
false?s
|
|
You can do this with the following piece of code :
|
[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));
}
|
|