WPF绑定错误:找不到附加的依赖项属性

Tus*_*har 1 c# wpf binding dependency-properties

我试图解决一些经常发生的数据绑定错误,如下所示

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')
Run Code Online (Sandbox Code Playgroud)

我有一个附加的依赖属性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));
Run Code Online (Sandbox Code Playgroud)

绑定在控件模板中的主题文件中设置

<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>
Run Code Online (Sandbox Code Playgroud)

控件模板用于内容控件,如此处所示(也可以在错误中看到)

<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>
Run Code Online (Sandbox Code Playgroud)

我花了很多时间试图弄清楚DependencyProperty是否缺少任何东西,或者绑定是否设置错误但无济于事.该应用程序按预期工作,但我试图摆脱这种绑定错误.如果我在XML中删除绑定但是这不是我正在寻找的解决方案,那么错误就会消失.绑定可能有问题.

我查看了其他几篇文章,阅读了关于依赖属性和绑定的资料和书籍章节,我找不到任何明显的错误.我甚至使用过snoop,但也没有从中获取任何其他信息.我看过的一些事情如下

BindingExpression路径错误:'object'上找不到属性

System.Windows.Data错误:40:BindingExpression路径错误:在对象上找不到属性

WPF错误40 BindingExpression路径错误:'对象'上找不到属性

http://wpftutorial.net/DependencyProperties.html

http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/

有没有办法确定声明/创建WPF绑定的位置?

WPF性能缓慢 - 许多DataItem = null绑定警告

什么可能出错的想法以及如何摆脱错误?

Rac*_*hel 6

WPF绑定错误并不总是最简单的读取错误,而是通过冒号/分号/句点分解,然后向后读取:

  • target属性是'NoTarget'(类型'对象')
  • target元素是'ContentControl'(Name ='HeaderBorder');
  • DataItem ='FrameViewModel'(HashCode = 55649279);
  • BindingExpression:路径=活动;
  • 在'object'''FrameViewModel'(HashCode = 55649279)'上找不到'活动'属性.
  • BindingExpression路径错误:
  • System.Windows.Data错误:40:

这可以理解为:

  • 在某处您尝试绑定一个名为的属性 NoTarget
  • 这家酒店坐落在一个<ContentControl>Name="HeaderBorder"
  • DataContextUI对象后面的DataItem()属于类型FrameViewModel
  • 您尝试绑定的属性被调用 "Active"
  • Active找不到该物业FrameViewModel
  • 其余的东西通常是可以忽略的

通常我建议你在代码中查找看起来有点像这样的东西:

<ContentControl Name="HeaderBorder" NoTarget="{Binding Active}" />
Run Code Online (Sandbox Code Playgroud)

这将是您的问题所在的错误消息

但是,在你的情况下,问题属性是NoTarget,这显然是WPF特有的东西(我一直在学习新东西!)与触发器有关.

Google快速搜索会返回此帖子,这表明问题是由于未使用正确的语法绑定到附加属性引起的.

尝试使用括号为附加属性编写绑定,如下所示

<DataTrigger Binding="{Binding Path=(local:Card.Active),...
Run Code Online (Sandbox Code Playgroud)

(我不完全确定这是否会起作用,因为我认为附加的依赖属性必须附加到UI对象,并且你将你的附加到一个Card听起来像数据对象的对象.但我可能是错的,或者误解了你的代码:))