Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit attached dependency property in wpf

I want to attach a dependency property to specific controls only.

If that is just one type, I can do this:

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached("MyProperty", typeof(object), typeof(ThisStaticWrapperClass));

public static object GetMyProperty(MyControl control)
{
    if (control == null) { throw new ArgumentNullException("control"); }

    return control.GetValue(MyPropertyProperty);
}

public static void SetMyProperty(MyControl control, object value)
{
    if (control == null) { throw new ArgumentNullException("control"); }

    control.SetValue(MyPropertyProperty, value);
}

(So: limit the Control type in the Get/Set-Methods)

But now I want to allow that property to get attached on a different type of Control, too.
You'd try to add an overload for both methods with that new type, but that fails to compile because of an "Unknown build error, Ambiguous match found."

So how can I limit my DependencyProperty to a selection of Controls?
(Note: In my specific case I need it for TextBox and ComboBox)

like image 814
ordag Avatar asked Jul 29 '11 14:07

ordag


People also ask

What is attached property and dependency property in WPF?

In WPF, most UI-related attached properties on WPF types are implemented as dependency properties. WPF attached properties that are implemented as dependency properties support dependency property concepts, such as property metadata including default values from metadata.

What are attached properties in WPF?

Attached properties are properties which can be set on any wpf object (basically, at least a DependencyObject) via the DependencyObject. SetValue method. The purpose for this mechanism is to "attach" to other objects information needed by parent objects, not the child objects themselves. For example, the Grid.

What is CoerceValueCallback?

The CoerceValueCallback for a dependency property is invoked any time that the property system or any other caller calls CoerceValue on a DependencyObject instance, specifying that property's identifier as the dp . Changes to the property value may have come from any possible participant in the property system.

What is dependency property in WPF interview?

Dependency property is a special kind of property introduced in WPF. This property helps in data binding the source and target object. Dependency property has been designed to reduce the memory footprint. These properties are static in nature.


1 Answers

Ambiguous match found.

...is normally thrown by GetMethod if there are multiple overloads and no type-signature has been specified (MSDN: More than one method is found with the specified name.). Basically the WPF-engine is only looking for one such method.

Why not check the type in the method body and throw an InvalidOperationException if it's not allowed?


Note however that those CLR-Wrappers should not include any code beside the setting and getting, if the propery is set in XAML they will be disregarded, try throwing an exception in the setter, it will not come up if you only use XAML to set the value.

Use a callback instead:

public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached
        (
            "MyProperty",
            typeof(object),
            typeof(ThisStaticWrapperClass),
            new UIPropertyMetadata(null, MyPropertyChanged) // <- This
        );

public static void MyPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    if (o is TextBox == false && o is ComboBox == false)
    {
        throw new InvalidOperationException("This property may only be set on TextBoxes and ComboBoxes.");
    }
}
like image 85
H.B. Avatar answered Oct 11 '22 20:10

H.B.