0

Say I have the following dependency property:

public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(
    "IsActive", 
    typeof(bool), 
    typeof(MyAttachedProp),
    new PropertyMetadata(false));

What is the difference between the following property accessor patterns:

public static void SetIsActive(DependencyObject obj, bool value)
{
    obj.SetValue(IsActiveProperty, value);
}

public static bool GetIsActive(DependencyObject obj)
{
    return (bool)obj.GetValue(IsActiveProperty);
}

vs

public bool IsActive
{
    get
    {
        return (bool)GetValue(IsActiveProperty);
    }
    set
    {
        SetValue(IsActiveProperty, value);
    }
}

I'm assuming which one you use depends on what you are authoring, e.g. an attached property/behaviour, converter, control, etc?

Clemens
  • 123,504
  • 12
  • 155
  • 268
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • 3
    The static Get/Set methods are used for [attached properties](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-properties-overview), the property is used for regular dependency properties. You'll have to use the latter one for your IsActiveProperty. – Clemens Jan 18 '18 at 09:39
  • 2
    If your `MyAttachedProp` class is supposed to contain attached properties like the name implies, then you should register them with `DependencyProperty.RegisterAttached` and you should probably turn the class into `public static class MyAttachedProp`, so you won't ever think of creating `public bool IsActive` inside. – grek40 Jan 18 '18 at 09:49

0 Answers0