1

We can achieve the binding by simply CLR property, so why do we need to use DP?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
pchajer
  • 1,584
  • 2
  • 13
  • 25

2 Answers2

1

When do you need DPs over CLRPs?

  • 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 DPs, its piece of cake.

decyclone
  • 30,394
  • 6
  • 63
  • 80
0

Typically these are declared in UserControls 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}" />
Community
  • 1
  • 1
Jay
  • 56,361
  • 10
  • 99
  • 123