WPF 绑定错误:未找到附加依赖属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20791499/
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
WPF Binding error: Attached Dependency Property not found
提问by Tushar
I am trying to resolve a number of frequently occurring data binding errors as given below
我正在尝试解决一些经常发生的数据绑定错误,如下所示
System.Windows.Data Error: 40 : BindingExpression path error: 'Active' property not found on 'object' ''FrameViewModel' (HashCode=55649279)'. BindingExpression:Path=Active; DataItem='FrameViewModel' (HashCode=55649279); target element is 'ContentControl' (Name='HeaderBorder'); target property is 'NoTarget' (type 'Object')
I have an attached dependency property Active
我有一个附加的依赖属性 Active
public bool Active
{
get
{
return (bool)GetValue(ActiveProperty);
}
set
{
SetValue(ActiveProperty, value);
}
}
public static readonly DependencyProperty ActiveProperty
= DependencyProperty.RegisterAttached("Active", typeof(bool), typeof(Card));
The binding is set in theme files in a control template
绑定在控件模板的主题文件中设置
<ControlTemplate x:Key="FrameHeader" TargetType="ContentControl">
.....
.....
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Active}" Value="True">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource SelectedHeaderBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Active}" Value="False">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource HeaderBrush}"/>
</DataTrigger>
</ControlTemplate.Triggers>
The control template is used in a content control as shown here (can be seen in the error as well)
控件模板用于内容控件,如下所示(也可以在错误中看到)
<ControlTemplate x:Key="Card" TargetType="{x:Type Button}">
<ContentControl Template="{DynamicResource FrameTemplate}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Template="{DynamicResource FrameHeader}" Grid.Row="0" x:Name="HeaderBorder" >
<Border Style="{DynamicResource BorderTop}">
<DockPanel>
<ContentPresenter x:Name="UpperLeftContent"
DockPanel.Dock="Left"
Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=icush:Frame},
Converter={StaticResource WatchListLockoutTimerVisibilityConverter},
Path=UpperLeftContent}"
Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=icush:Frame},
Converter={StaticResource WatchListLockoutTimerConverter},
ConverterParameter={StaticResource LockoutTimerRadius},
Path=UpperLeftContent}"
/>
<TextBlock x:Name="Header"
Style="{DynamicResource Header}"
Text="{Binding Path=Title}" />
</DockPanel>
</Border>
</ContentControl>
<ContentControl Template="{DynamicResource FrameBody}" Grid.Row="1" Content="{TemplateBinding Content}"/>
</Grid>
</ContentControl>
</ControlTemplate>
I have spent a lot of time trying to figure out if the DependencyProperty is missing anything or if the binding is set incorrectly but to no avail. The application works as expected but I'm trying to get rid of such binding errors. The error goes away if I remove the binding in XML but that is not the solution I'm looking for. There might be something wrong with the binding.
我花了很多时间试图弄清楚 DependencyProperty 是否缺少任何内容,或者绑定设置是否错误但无济于事。该应用程序按预期工作,但我正在尝试摆脱此类绑定错误。如果我删除 XML 中的绑定,错误就会消失,但这不是我正在寻找的解决方案。绑定可能有问题。
I have looked at several other posts, read material and book chapters on Dependency Properties and binding and I can't find any obvious mistake. I have even used snoop but didn't get any additional information from that too. Some of the things I've looked at are given below
我查看了其他几篇文章,阅读了关于依赖属性和绑定的材料和书籍章节,但我找不到任何明显的错误。我什至使用过 snoop,但也没有从中获得任何其他信息。下面给出了我看过的一些内容
BindingExpression path error: property not found on 'object'
BindingExpression 路径错误:在“对象”上找不到属性
System.Windows.Data Error: 40 : BindingExpression path error: property not found on object
System.Windows.Data 错误:40:BindingExpression 路径错误:在对象上找不到属性
WPF Error 40 BindingExpression path error: property not found on 'object'
WPF 错误 40 BindingExpression 路径错误:在“对象”上找不到属性
http://wpftutorial.net/DependencyProperties.html
http://wpftutorial.net/DependencyProperties.html
http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/
http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/
Is there a way to determine where a WPF Binding is declared/created?
WPF slow performance - many DataItem=null binding warnings
WPF 性能缓慢 - 许多 DataItem=null 绑定警告
Any ideas of what could be going wrong and how I can get rid of the error?
关于可能出什么问题以及如何摆脱错误的任何想法?
采纳答案by Tushar
So the problem here was that for attached properties you need to enclose the attached property in parenthesis, something which was missing in my code. MSDN link, found in the postpointed to by @Rachel, refers to this requirement.
所以这里的问题是,对于附加属性,您需要将附加属性括在括号中,这是我的代码中缺少的内容。在@Rachel 指向的帖子中找到的MSDN链接是指此要求。
I was also missing the namespace prefix for the card class. After creating the namespace prefix localI changed the code to the following
我还缺少卡片类的命名空间前缀。创建命名空间前缀后,local我将代码更改为以下内容
<DataTrigger Binding="{Binding Path=(local:Card.Active)}" Value="True">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource SelectedHeaderBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(local:Card.Active)}" Value="False">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource HeaderBrush}"/>
</DataTrigger>
This is all that was needed!
这就是所需要的!
回答by Rachel
WPF binding errors aren't always the easiest thing to read, but break it up by the colons/semi-colons/periods, and read it backwards:
WPF 绑定错误并不总是最容易阅读的事情,但可以用冒号/分号/句点将其分解,然后向后阅读:
- target property is 'NoTarget' (type 'Object')
- target element is 'ContentControl' (Name='HeaderBorder');
- DataItem='FrameViewModel' (HashCode=55649279);
- BindingExpression:Path=Active;
- 'Active' property not found on 'object' ''FrameViewModel' (HashCode=55649279)'.
- BindingExpression path error:
- System.Windows.Data Error: 40 :
- 目标属性是“NoTarget”(类型“Object”)
- 目标元素是 'ContentControl' (Name='HeaderBorder');
- DataItem='FrameViewModel' (HashCode=55649279);
- BindingExpression:Path=Active;
- 在“对象”“FrameViewModel”(HashCode=55649279)上找不到“Active”属性。
- BindingExpression 路径错误:
- System.Windows.Data 错误:40:
This can be read as:
这可以理解为:
- Somewhere you are trying to bind a property called
NoTarget - This property is located on a
<ContentControl>withName="HeaderBorder" - The DataItem (
DataContext) behind that UI object is of typeFrameViewModel - The property you are trying to bind is called
"Active" - The
Activeproperty is not found onFrameViewModel - The rest of the stuff is usually ignorable
- 在某个地方您试图绑定一个名为
NoTarget - 这家酒店坐落在一个
<ContentControl>带Name="HeaderBorder" - 该
DataContextUI 对象背后的 DataItem ( ) 是类型FrameViewModel - 您尝试绑定的属性称为
"Active" Active找不到该属性FrameViewModel- 其余的东西通常可以忽略
Normally I'd suggest you look in your code for something that looks a bit like this:
通常我会建议你查看你的代码,看起来有点像这样:
<ContentControl Name="HeaderBorder" NoTarget="{Binding Active}" />
and that will be where your problem is for this error message
这将是此错误消息的问题所在
But, in your case the problem property is NoTarget, which is apparently something special to WPF (I learn something new all the time!) related to the triggers.
但是,在您的情况下,问题属性是NoTarget,这对于 WPF 来说显然是特殊的(我一直在学习新东西!)与触发器相关。
A Google quick search returns this post, which suggests the problem is caused by not using the correct syntax for binding to an attached property.
谷歌快速搜索返回了这篇文章,这表明问题是由于没有使用正确的语法绑定到附加属性引起的。
Try writing the binding for your attached property with parenthesis, like this
尝试使用括号为附加属性编写绑定,如下所示
<DataTrigger Binding="{Binding Path=(local:Card.Active),...
(I'm not entirely sure if this will work because I thought attached dependency properties had to be attached to UI objects, and you have yours attached to a Cardobject which sounds like a data object. But I could be wrong, or have misunderstood your code :))
(我不完全确定这是否可行,因为我认为附加的依赖属性必须附加到 UI 对象,而您将您的附加到一个Card听起来像数据对象的对象。但我可能是错的,或者误解了您的代码 :))

