Dependency properties have more uses in WPF and having advanced features. WPF Classes used more properties rather than methods.
Before going to Dependency properties in WPF, we need to understand our normal DotNet Properties.
Dot Net Properties
Class A
{
private int _myvalue;
public int MYVALUE
{
get{return _myvalue;}
set{ _myvalue=Value;}
}
}
A property must associated with private field in class and represent that field to other classes. This field is backing field.
backing field nothing but private variable which holds the property value.
Assign value
When ever value get assigned to the property, value is passed to Set accessor reflect value to backing field.
Read Value
When ever value get read from the property, Get accessor just return the value from backing field
Dot net property can be easily assigned and read directly from private variables whereas WPF Dependency properties value is resolved dynamically
When calling GETVALUE() method that is inherited from Dependency Object.
WPF -Dependency Properties
Advantages
Reduce Memory Usage:
If we set value to parent Control it will easily reflect to all child controls. modified properties present in the instance. The Default value stored once with dependency property.
Value Inheritance:
Dependency property value is resolved using value resolution strategy. if no local value being set, the dependency property will travel from top to bottom until find its value.
Change Notification:
Dependency properties having new feature built in notification whenever a value modify it will notify the changes. By registering a callback data in the mechanism. Used in DataBinding techniques.
Features
Value of dependency property is not stored in field of object, but it will stored in dictionary of keys and value will provided by the base class Dependency Object.
VALUE RESOLUTION STRATEGY
Example
XAML File which set FontWeignt bold it will reflect to all child controls unless explicitly modification/Changes in the controls.
<Window X:Class="Inherit.Font.Window1"....>
<GroupBox FontWeight="Bold">
<GroupBox.Header>
Buttons
</GroupBox.Header>
</GroupBox>
<StackPanel>
<Button FontWeight="Medium">Update</Button>
<Button>Reset</Button>
</StackPanel>
</Window>
Sample Dependency property
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty = DependencyProperty.Register( "CurrentTime", typeof(DateTime), typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));
// .NET Property wrapper
public DateTime CurrentTime
{
get { return (DateTime)GetValue(CurrentTimeProperty); }
set { SetValue(CurrentTimeProperty, value); }
}
Before going to Dependency properties in WPF, we need to understand our normal DotNet Properties.
Dot Net Properties
Class A
{
private int _myvalue;
public int MYVALUE
{
get{return _myvalue;}
set{ _myvalue=Value;}
}
}
A property must associated with private field in class and represent that field to other classes. This field is backing field.
backing field nothing but private variable which holds the property value.
Assign value
When ever value get assigned to the property, value is passed to Set accessor reflect value to backing field.
Read Value
When ever value get read from the property, Get accessor just return the value from backing field
Dot net property can be easily assigned and read directly from private variables whereas WPF Dependency properties value is resolved dynamically
When calling GETVALUE() method that is inherited from Dependency Object.
WPF -Dependency Properties
Advantages
Reduce Memory Usage:
If we set value to parent Control it will easily reflect to all child controls. modified properties present in the instance. The Default value stored once with dependency property.
Value Inheritance:
Dependency property value is resolved using value resolution strategy. if no local value being set, the dependency property will travel from top to bottom until find its value.
Change Notification:
Dependency properties having new feature built in notification whenever a value modify it will notify the changes. By registering a callback data in the mechanism. Used in DataBinding techniques.
Features
Value of dependency property is not stored in field of object, but it will stored in dictionary of keys and value will provided by the base class Dependency Object.
VALUE RESOLUTION STRATEGY
Example
XAML File which set FontWeignt bold it will reflect to all child controls unless explicitly modification/Changes in the controls.
<Window X:Class="Inherit.Font.Window1"....>
<GroupBox FontWeight="Bold">
<GroupBox.Header>
Buttons
</GroupBox>
<StackPanel>
<Button FontWeight="Medium">Update</Button>
<Button>Reset</Button>
</StackPanel>
</Window>
Sample Dependency property
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty = DependencyProperty.Register( "CurrentTime", typeof(DateTime), typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));
// .NET Property wrapper
public DateTime CurrentTime
{
get { return (DateTime)GetValue(CurrentTimeProperty); }
set { SetValue(CurrentTimeProperty, value); }
}
No comments :
Post a Comment