首先让我说我已经在WPF工作了很长时间。我已经定义并使用了许多AttachedProperties,但这个让我感到难过。也许我只是忽略了一些简单的东西,我需要别人的眼睛才能看到它。
我有一个AttachedProperty,就像任何其他AttachedProperty一样定义:
public static readonly DependencyProperty NullAdornerStringProperty =
DependencyProperty.RegisterAttached(
"NullAdornerString",
typeof(string),
typeof(NullTextAdorner),
new FrameworkPropertyMetadata(null, OnNullAdornerStringChanged));
public static void SetNullAdornerStringProperty(DependencyObject obj, string nullAdornerString)
{
obj.SetValue(NullAdornerStringProperty, nullAdornerString);
}
public static string GetNullAdornerStringProperty(DependencyObject obj)
{
return (string)obj.GetValue(NullAdornerStringProperty);
}
注意:NullTextAdorner派生自DependencyOject,并且我的属性没有错误更改了事件处理程序。
但是,当我在XAML中使用该AP时,我只能将其引用为“NullAdornerStringProperty”,这是不寻常的,因为实际的属性名称被注册为“NullAdornerString”。我在过去制作的任何其他AP总是在XAML中通过注册名称引用,而不是该属性的全名。
我实际上可以像这样在xaml中为属性分配一个静态字符串,它在运行时运行良好:
<igWPF:XamTextEditor OSAdorners:NullTextAdorner.NullAdornerStringProperty="The Value is Null!!" />
但是我不能用绑定表达式设置它
<igWPF:XamTextEditor OSAdorners:NullTextAdorner.NullAdornerStringProperty="{Binding SomeProperty}" />
这给了我一个运行时错误:
A 'Binding' cannot be set on the 'SetNullAdornerStringProperty' property of type 'XamTextEditor'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
如果我强制XAML应该是它应该是什么,
<igWPF:XamTextEditor OSAdorners:NullTextAdorner.NullAdornerString="{Binding SomeProperty}"
VS告诉我它无法找到属性:
The attachable property 'NullAdornerString' was not found in type 'NullTextAdorner'.
我设置属性的控件类型无关紧要。我总是得到这种行为。 这是在具有其他附加属性的程序集中,这些属性可以正常工作并且可以使用绑定。我甚至将这个AP移动到了一个其他AP工作的类,但仍然有这种行为。 我希望有人会看到我目前无视的东西。
提前致谢。
答案 0 :(得分:3)
哦,因为什么时候我的朋友定义了附加属性?
您没有遵循命名惯例。
SetNullAdornerStringProperty - 属性部分将被删除。
GetNullAdornerStringProperty也是如此 - 删除属性部分。