Reputation: 83
What is the real use of use/create Dependency property during real scenario?
If we are using notification purpose then i will choose InotifyChanged Property.
I have never seen/used real use of Dependency Property in real time projects?
Can somebody tell me where DP is required in Realtime Scenrios?
Upvotes: 0
Views: 156
Reputation: 4808
If you have created a custom control with properties that you want to be bindable (e.g. following code), you cannot use INotifyPropertyChanged
and you must use a DependencyProperty
.
Assume you have a UserControl
like this:
public partial class MyUserControl : UserControl
{
public List<ItemViewModel> ItemsSource
{
get { return (List<ItemViewModel>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(List<ItemViewModel>), typeof(MyUserControl ), new PropertyMetadata(null));
}
Then you can set a binding to ItemsSource
(which is a DependencyProperty) in your main window this way:
<uc:MyUserControl ItemsSource="{Binding MyItems}" />
As Summary:
In general, a binding looks like this: T = "{Binding S}"
T
is the Target of the binding.S
is the Source of the binding.
T
is only allowed to be a DependencyProperty. e.g:
MyDependencyProperty="{Binding something}"
S
is often an INPC-Property. e.g:
something="{Binding MyINPCProperty}"
Upvotes: 2
Reputation: 128145
From MSDN:
The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs. These other inputs might include system properties such as themes and user preference, just-in-time property determination mechanisms such as data binding and animations/storyboards, multiple-use templates such as resources and styles, or values known through parent-child relationships with other elements in the element tree. In addition, a dependency property can be implemented to provide self-contained validation, default values, callbacks that monitor changes to other properties, and a system that can coerce property values based on potentially runtime information. Derived classes can also change some specific characteristics of an existing property by overriding dependency property metadata, rather than overriding the actual implementation of existing properties or creating new properties.
Upvotes: 1