This is an old revision of the document!


Dependency Properties

If data binding is required to certain properties, then these need to be implemented as Dependency Properties.

Here is a good example (from: C# is Easy: Dependency Property):

public class Button : ButtonBase
{
  // The dependency property
  public static readonly DependencyProperty IsCancelProperty;
 
  // Constructor
  static Button()
  {
    // Register the property
    Button.IsCancelProperty = DependencyProperty.Register(“IsCancel”,
        typeof(bool), typeof(Button),
        new FrameworkPropertyMetadata(false,
        new PropertyChangedCallback(OnIsDefaultChanged)));}
 
  // A .NET property wrapper (not must)
  public bool IsCancel
  {
    get { return (bool)GetValue(Button.IsCancelProperty ); }
    set { SetValue(Button.IsCancelProperty , value); }
  }
 
  // A property changed callback (not must)
  private static void OnIsCancelChanged(
    DependencyObject o, DependencyPropertyChangedEventArgs e) {}}

IMPORTANT: The accessor property is fine to use, but the XAML code will only call the SetValue() and GetValue() methods directly, so make sure that no other code gets added to the accessor property, to make sure both work the same.

See Also