A'绑定'只能在DependencyObject的一个设定的DependencyProperty [英] A 'Binding' can only be set on a DependencyProperty of a DependencyObject
问题描述
从基于文本框
自定义的控制,我创建了一个名为属性项目
,以这种方式:
From a custom control based on TextBox
, I created a property named Items
, in this way:
public class NewTextBox : TextBox
{
public ItemCollection Items { get; set; }
}
在使用XAML自定义控件,我不能属性,因为它会引发异常绑定一个'绑定'只能在DependencyObject的一个的DependencyProperty设置。
When using the custom control in XAML, I cannot bind the property because it raises exception "A 'Binding' can only be set on a DependencyProperty of a DependencyObject.".
我要如何解决这个例外?
How do I solve this exception?
推荐答案
作为一个方面说明,这也是值得注意的是,如果你复制你会得到这些绑定错误和对象之间的粘贴和忘记改变第二个的typeof(对象)
语句。
As a side note, it is also worth noting that you will get these binding errors if you copy and paste between objects and forget to change the second typeof(Object)
statement.
我无法找出一个好的小时为什么我收到此错误因为一切似乎是界定和正确的。我已经动了我的属性放到一个用户控件,因为我想从一个单一的集合到一个列表中去。因此:
I couldn't figure out for a good hour why I was getting this error as everything appeared to be defined and correct. I'd moved my properties into a usercontrol as I wanted to go from a single set to a list. Thus:
public static readonly DependencyProperty FoldersProperty = DependencyProperty.Register("Folders", typeof(OutlookFolders), typeof(MainWindow), new FrameworkPropertyMetadata(new OutlookFolders()));
public OutlookFolders Folders
{
get { return GetValue(FoldersProperty) as OutlookFolders; }
set { SetValue(FoldersProperty, value); }
}
应该成为:
public static readonly DependencyProperty FoldersProperty = DependencyProperty.Register("Folders", typeof(OutlookFolders), typeof(SavedFolderControl), new FrameworkPropertyMetadata(new OutlookFolders()));
public OutlookFolders Folders
{
get { return GetValue(FoldersProperty) as OutlookFolders; }
set { SetValue(FoldersProperty, value); }
}
直到我做这个改变我不断收到错误: A'绑定'不能在类型'SavedFolderControl财产文件夹进行设置。 A'绑定'只能在DependencyObject的一个的DependencyProperty设置。
这篇关于A'绑定'只能在DependencyObject的一个设定的DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!