在WPF限制附加依赖属性属性、WPF

2023-09-03 15:57:06 作者:*舊倳偂歡゛

我要附加一个依赖属性只有特定的控制。

如果这仅仅是一种类型的,我可以做到这一点:

 公共静态只读的DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached(myProperty的的typeof(对象)的typeof(ThisStaticWrapperClass));

公共静态对象GetMyProperty(MyControl控制)
{
    如果(控制== NULL){抛出新ArgumentNullException(控制); }

    返回control.GetValue(MyPropertyProperty);
}

公共静态无效SetMyProperty(MyControl控制目标值)
{
    如果(控制== NULL){抛出新ArgumentNullException(控制); }

    control.SetValue(MyPropertyProperty,价值);
}
 

(所以:限制控制键入get / set方法,方法)的

但现在我想允许的属性来获取安装在不同类型的控制,太。照片 你会尝试添加过载的两种方法与新的类型,但失败,因为一个的编译未知的生成错误,不明确的比赛中。的

所以,我怎么能限制我的的DependencyProperty 来选择控制第? (注:在我的特定情况下,我需要它文本框组合框)的

解决方案   

暧昧找到匹配。

...一般是由 GetMethod的 如果有多个过载和无类型的签名已被指定(MSDN:以上的方法找到具有指定名称) 。基本上,WPF引擎只想找一个这样的方法。

为什么不检查的类型,在方法体,并抛出一个 InvalidOperationException异常如果它不允许?

但是请注意,这些CLR的包装器不应该包括任何code 设置旁边和得到,如果属性为XAML中的设置,他们将被忽略尝试在setter方法​​抛出一个异常,也不会拿出如果你只使用XAML来设置的值。

.NET中WPF的依赖属性

使用一个回调来代替:

 公共静态只读的DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached
        (
            myProperty的,
            typeof运算(对象),
            typeof运算(ThisStaticWrapperClass)
            新UIPropertyMetadata(空,MyPropertyChanged)//<  - 这
        );

公共静态无效MyPropertyChanged(DependencyObject的0,DependencyPropertyChangedEventArgs E)
{
    如果(O是文本框==假放;&放大器; o为组合框==假)
    {
        抛出新的InvalidOperationException异常(该属性只能在文本框和组合框进行设置。);
    }
}
 

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)

解决方案

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.");
    }
}