【发布时间】:2023-03-13 07:27:01
【问题描述】:
为另一个依赖属性问题道歉
以下问题:
XAML Binding on Dependency Property
A 'Binding' can only be set on a DependencyProperty of a DependencyObject
还有本教程:
http://wpftutorial.net/DependencyProperties.html
我也有类似的问题。在尝试了解决方案之后并没有太大变化。
我做了一个控件库:WpfCustomControlLibrary1
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
并添加了一个带有所需回调的依赖属性:
FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.Inherits, PropertyChangedCallbackMethod, OnDictionaryCoerce,false);
public static readonly DependencyProperty DictionaryProperty = DependencyProperty.Register("DictionaryProperty",typeof(Dictionary<string,int>),
typeof(CustomControl1),new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.Inherits));
private Dictionary<string, int> _Dictionary;
public static void PropertyChangedCallbackMethod(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// I am not entirely sure what code should be implemented here, leaving it blank for now
}
private static Object OnDictionaryCoerce(DependencyObject sender,Object data)
{
return data;
}
private static bool OnValidate(object data)
{
return data is Dictionary<string, int>;
}
public Dictionary<string, int> Dictionary
{
get
{
return _Dictionary;
}
set
{
_Dictionary = value;
}
}
}
}
然后我尝试将属性设置为应用程序中的绑定,但收到错误消息:
如何将我的依赖属性设置为依赖对象?还是他们设置绑定到依赖属性的一种方式?
依赖属性的基类不是依赖对象吗?
【问题讨论】:
-
当你注册一个依赖属性时,你应该注册你想使用的名字
"Dictionary"而不是"DictionaryProperty"。如果您使用的是 Visual Studio,您可能有权访问内置的 sn-p。在代码中输入propdp并点击两次tab。 -
请解释否决票