We can achieve the binding by simply CLR property, so why do we need to use DP?
Asked
Active
Viewed 850 times
1
-
Possible duplicate of http://stackoverflow.com/questions/4347135/demystifying-dependency-properties – Simon Mourier Dec 29 '10 at 16:52
2 Answers
1
When do you need DP
s over CLRP
s?
- When you need
binding
- when you need
property value change callback
(Default Implementation) - When you need
property value validation
- When you need
animation
- When you need
property value inheritance
- When you need to
attach a property value
to another element (Attached Property, but still) - When you need
styling
Some of these can be implemented in CLR
properties. But, with DP
s, its piece of cake.

decyclone
- 30,394
- 6
- 63
- 80
0
Typically these are declared in UserControl
s and derived controls.
You can bind to a CLR property, but you can't bind with a CLR property; you'll need a dependency property to do any binding.
Edit (in response to comment)
Let's say you need a TextBox, but you want to customize it to have different behaviour in "EditMode" and "ReadMode". You'll need to either create a derived class or a UserControl
; in either case you'll add a DependencyPropery.
public class TextBoxWithModes : TextBox
{
public bool EditMode
{
get { return (bool) GetValue(EditModeProperty); }
set { SetValue(EditModeProperty, value); }
}
public static readonly DependencyProperty EditModeProperty = DependencyProperty.Register(
"EditMode", typeof (bool), typeof (TextBoxWithModes));
}
With this in place, you can declare it in XAML:
<Namespace:TextBoxWithModes Text="enter text here"
Width="200"
HorizontalAlignment="Center"
EditMode="{Binding IsChecked, ElementName=editModeCheckBox}" />