what is dependency properties in wpf

Posted by Srinup97 under WPF on 10/1/2013 | Points: 10 | Views : 1894 | Status : [Member] | Replies : 2
Hi,
What is dependency properties in wpf how to use this

Please tell me with examples

Thanks

srinivasp


Responses

Posted by: Bandi on: 10/1/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
The dependency property
While you using the dependency property it makes the most sense in elements-classes that have a visual appearance (UIelements).

Pros:

1) WPF do the logic stuff for you
2) Some mechanism like animation use only dependency property
3) 'Fits' ViewModel style

Cons:

1) You need to derive form DependencyObject
2) A bit awkward for simple stuff

Sample:

public static class StoryBoardHelper
{
public static DependencyObject GetTarget(Timeline timeline)
{
if (timeline == null)
throw new ArgumentNullException("timeline");

return timeline.GetValue(TargetProperty) as DependencyObject;
}

public static void SetTarget(Timeline timeline, DependencyObject value)
{
if (timeline == null)
throw new ArgumentNullException("timeline");

timeline.SetValue(TargetProperty, value);
}

public static readonly DependencyProperty TargetProperty =
DependencyProperty.RegisterAttached(
"Target",
typeof(DependencyObject),
typeof(Timeline),
new PropertyMetadata(null, OnTargetPropertyChanged));

private static void OnTargetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Storyboard.SetTarget(d as Timeline, e.NewValue as DependencyObject);
}
}


Refer
http://www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property
http://stackoverflow.com/questions/3551204/when-to-use-a-wpf-dependency-property-versus-inotifypropertychanged

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Srinup97, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/1/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
References:
http://www.developerfusion.com/article/84469/wpf-dependency-properties/
http://stackoverflow.com/questions/1723756/why-dependency-properties
http://stackoverflow.com/questions/771833/how-to-bind-to-a-wpf-dependency-property-when-the-datacontext-of-the-page-is-use

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Srinup97, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response