C# 只能在 DependencyObject 的 DependencyProperty 上设置“绑定”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11435781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A 'Binding' can only be set on a DependencyProperty of a DependencyObject
提问by mgottschild
From a custom control based on TextBox, I created a property named Items, in this way:
从基于 的自定义控件中TextBox,我Items以这种方式创建了一个名为 的属性:
public class NewTextBox : TextBox
{
public ItemCollection Items { get; set; }
}
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.".
在 XAML 中使用自定义控件时,我无法绑定该属性,因为它引发异常“只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。
How do I solve this exception?
我该如何解决这个异常?
回答by mgottschild
To solve this exception you need to change the property Itemsand add a DependencyPropertythat will work as a "link" in XAML. The class will be:
要解决此异常,您需要更改属性Items并添加DependencyProperty将在 XAML 中用作“链接”的 。课程将是:
public class AutocompleteTextBox : TextBox
{
public ItemCollection Items
{
get {
return (ItemCollection)GetValue(ItemsProperty); }
set {
SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register(
"Items",
typeof(ItemCollection),
typeof(AutocompleteTextBox),
new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged));
private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// AutocompleteTextBox source = d as AutocompleteTextBox;
// Do something...
}
回答by netniV
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.
作为旁注,还值得注意的是,如果您在对象之间进行复制和粘贴而忘记更改第二条typeof(Object)语句,则会出现这些绑定错误。
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); }
}
Should have become:
应该变成:
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); }
}
Until I did this change I kept receiving the error:
A 'Binding' cannot be set on the property 'Folders' of type 'SavedFolderControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
在我进行此更改之前,我一直收到错误消息:
A 'Binding' cannot be set on the property 'Folders' of type 'SavedFolderControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
回答by Prof Von Lemongargle
Another potential cause of this is when you provide a bad type for the default value in the metadata.
For instance:
另一个可能的原因是当您为元数据中的默认值提供错误类型时。
例如:
new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged)
would throw this error if you wrote instead:
如果您改为编写,则会抛出此错误:
new PropertyMetadata(false, OnItemsPropertyChanged)
This can also happen if you are copying and pasting from a code source.
如果您从代码源复制和粘贴,也会发生这种情况。
回答by Oliver
Here's another gotcha: Ensure that the string in the first argument of DependencyProperty.Register()matches the name of the related property.
这是另一个问题:确保第一个参数中的字符串DependencyProperty.Register()与相关属性的名称匹配。
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register(
"TheItems", // This is wrong
typeof(ItemCollection),
typeof(AutocompleteTextBox),
new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged));
I ran into this issue when I renamed my property without changing the string.
当我在不更改字符串的情况下重命名我的属性时遇到了这个问题。
回答by EricG
I had the (runtime + designtime) message:
我收到了(运行时 + 设计时)消息:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: A 'Binding' cannot be set on the 'Property' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
PresentationFramework.dll 中发生类型为“System.Windows.Markup.XamlParseException”的未处理异常
附加信息:无法在“触发器”类型的“属性”属性上设置“绑定”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。
Where I was smart enough to define a Trigger on a VM property..
我足够聪明,可以在 VM 属性上定义触发器。
// incorrect.. cannot have Trigger for VM property
<Trigger Property="{Binding IsExpanded}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</Trigger>
Which should of course be a datatrigger (which uses Binding instead of Property)
这当然应该是一个数据触发器(它使用绑定而不是属性)
<DataTrigger Binding="{Binding IsExpanded}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
Triggers are typically for Controls (Button, TextBox, FrameworkElement etc.) properties.
触发器通常用于控件(按钮、文本框、框架元素等)属性。
回答by Pic Mickael
One thing I noticed, and I am not sure it is mentioned anywhere, is that the name of your DependencyProperty must matchyour property name
我注意到的一件事,我不确定它是否在任何地方被提及,是您的 DependencyProperty 的名称必须与您的属性名称相匹配
If your property name is Items, then you DependencyPropertymustbe ItemsProperty
如果您的属性名称是Items,那么您的DependencyProperty必须是ItemsProperty
In my case, as soon as I matched them the error went away
就我而言,只要我匹配它们,错误就消失了

