2

How can I create a simple bool dependency property IsInput. This value can only be set to true or false when the class is created in code. Seems rather simple but ive searched around online and haven't found a clear example.

I've seen examples like this one below online but I'm not quite clear on what I would duplicate to create my own bool dependency property correctly.

public static readonly DependencyProperty AncestorProperty =
    DependencyProperty.Register("Ancestor", typeof(FrameworkElement), typeof(MyItem),
        new FrameworkPropertyMetadata(Ancestor_PropertyChanged));

/// <summary>
/// Event raised when 'Ancestor' property has changed.
/// </summary>
private static void Ancestor_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MyItem c = (MyItem)d;
    c.UpdateHotspot();
}
appa yip yip
  • 1,404
  • 1
  • 17
  • 38
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

1 Answers1

4

The second parameter of the Register method is the type of the property, i.e. bool, while the third parameter is the so-called owner type, which is the type that declares the property (MyControl in the example below).

For a complete dependency property declaration you also need to declare the "wrapper" property with a getter and a setter that call the dependency property's GetValue and SetValue methods.

public static readonly DependencyProperty IsInputProperty =
    DependencyProperty.Register("IsInput", typeof(bool), typeof(MyControl),
        new FrameworkPropertyMetadata(IsInputPropertyChanged));

/// <summary>
/// CLR wrapper for the 'IsInput' dependency property.
/// </summary>
public bool IsInput
{
    get { return (bool)GetValue(IsInputProperty); }
    set { SetValue(IsInputProperty, value); }
}

/// <summary>
/// Callback called when 'IsInput' property has changed.
/// </summary>
private static void IsInputPropertyChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    bool b = (bool)e.NewValue;
    //TODO
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
  • Not sure what should be wrong about. Could you please explain? – Flat Eric Jan 22 '16 at 16:10
  • Read the [documentation](https://msdn.microsoft.com/en-us/library/ms597500(v=vs.100).aspx): "*ownerType*: The owner type that is registering the dependency property." – Clemens Jan 22 '16 at 16:12
  • Take a look at [WPF Dependency Properties: Why do I need to specify an Owner Type?](http://stackoverflow.com/questions/868768/wpf-dependency-properties-why-do-i-need-to-specify-an-owner-type), it is almost always correct to use the actual owner `Type` rather than a different class. – Lukazoid Jan 22 '16 at 16:15
  • @Lukazoid Actually, it is never correct to use any other type. Besides that there is no reason to do so. If you declare a property in a different type than the one where the property is applied, use an attached property. – Clemens Jan 22 '16 at 16:17
  • @Clemens That is true, I would be using an attached property. I assumed the accepted and upvoted answer on my link was correct and just something I had never used. – Lukazoid Jan 22 '16 at 16:26