-1

Possible Duplicate:
When to use Dependency Properties

i read about WPF dependency property but just could not understand it well what is dependency property and why it is required. when to use dependency property i means what kind of situation one should go for dependency property.

here is a sample code for dependency property

public static readonly DependencyProperty IsSpinningProperty = 
   DependencyProperty.Register(
   "IsSpinning", typeof(Boolean),

);

public bool IsSpinning 
{
 get { return (bool)GetValue(IsSpinningProperty); }
 set { SetValue(IsSpinningProperty, value); }
}

please help me to understand the dependency property with a easy sample code and also show me how application will be benifited. when it is required etc.

thanks

Community
  • 1
  • 1
Mou
  • 15,673
  • 43
  • 156
  • 275
  • 3
    You are trying to ask too many questions all in one go, and almost all of the questions are already answered by other questions. For example the right hand "Related" questions column already has "Why dependency properties" and "When to use dependency properties" – Rob McCready Jul 05 '11 at 06:46

2 Answers2

2

Simply put, a dependency property is used when it is involved in data binding (probably in some XAML code), or if you want that property to be set in XAML. It has significant overhead in comparison to regular C# properties, so if you don't need it, stick to regular properties.

K Mehta
  • 10,323
  • 4
  • 46
  • 76
  • I'm not sure I agree with the "overhead" statement. Dependency properties were designed to minimize the *memory* overhead of the property system, making it possible to instantiate a large tree of objects while using less memory than traditional properties. They do have conceptual overhead and require a bit more code to implement, but performance-wise, I don't think they are a problem. I'd also qualify that Dependency Properties are required when you want the property to be the originator of a binding, as would be done by declaring a binding in XAML. – NathanAW Jul 05 '11 at 16:56
  • I would strongly suggest that you reread how dependency properties where implemented, and then compare them to how regular properties work. Dependency properties were designed to "minimize" overhead. Regular properties have no overhead. The entire notification structure does not come free of cost with dependency properties. As for your second point, i mentioned that they are used when the property is involved in data binding; since my second argument, which suggested their use in declaration started with "or" the former implied that the property must be the originator of a binding. – K Mehta Jul 05 '11 at 19:35
1

The following link gives a good overview of

  • how a Dependency Property differs from a normal CLR property, and
  • what advantages are Dependency Properties offer.

MSDN: Dependency Properties Overview

I suggest that you try to read through this article and then return with more specific questions, if there is anything in particular that still confuses you.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • no link found for 1) how a Dependency Property differs from a normal CLR property 2) what advantages are Dependency Properties offer. – Mou Jul 05 '11 at 07:23
  • @user728750: The link is behind the phrase "Dependency Property Overview". The section "*Property Functionality Provided by a Dependency Property*" shows which features are offered by Dependency Properties which you do not get with "normal" CLR properties. – Heinzi Jul 05 '11 at 07:44