Wpf 将曾孙的财产与祖先绑定

Wpf 将曾孙的财产与祖先绑定,wpf,xaml,binding,Wpf,Xaml,Binding,我甚至不知道该如何表达,所以如果标题让人困惑,我很抱歉。。我的XAML(简化)如下所示: <UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView" xmlns:FreqReserve="clr-namespace:PBA.Application.Client.UserControls.UserControls.FreqReserve"> ... &l

我甚至不知道该如何表达,所以如果标题让人困惑,我很抱歉。。我的XAML(简化)如下所示:

<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView" xmlns:FreqReserve="clr-namespace:PBA.Application.Client.UserControls.UserControls.FreqReserve">
...
    <DockPanel>
        <UserControls:LegendControl>
            <UserControls:LegendControl.Items>
                <UserControls:LegendItem Visibility="{Binding Path=IsDirtyVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FreqReserve:OverView}}, Converter={StaticResource btvc}}" Identifier="Pink" Text="Ikke sendt"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="Yellow" Text="Sendt"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="LightGreen" Text="Accepteret"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="White" Text="Ikke accepteret"></UserControls:LegendItem>
            </UserControls:LegendControl.Items>
        </UserControls:LegendControl>
    </DockPanel>
</UserControl>
<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView"
             ...
             xmlns:local="clr-namespace:PBA.Application.Client.UserControls.UserControls">

    ...
       <DockPanel>
        <UserControls:LegendControl>
            <UserControls:LegendControl.Items>
                <UserControls:LegendItem IsVisible="{Binding Path=IsDirtyVisible, 
                    RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type local:FreqReserve.OverView}}}" 
                    Identifier="Pink" 
                    Text="Ikke sendt"></UserControls:LegendItem>

               ....         

        </UserControls:LegendControl>
    </DockPanel>
</UserControl>

...
LegendItem列表在legendcontrol中模板化


绑定表达式失败,出现System.Windows.Data错误:4。我尝试使用elementname,但结果相同。我猜这与LegendItems没有直接出现在视觉树中有关,但我不知道(我知道WPF菜鸟)。我做错了什么

您的AncestorType中有一个输入错误。您想说FreqReserve.OverView。此外,还必须引用UserControl中定义的库名称空间

大概是这样的:

<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView" xmlns:FreqReserve="clr-namespace:PBA.Application.Client.UserControls.UserControls.FreqReserve">
...
    <DockPanel>
        <UserControls:LegendControl>
            <UserControls:LegendControl.Items>
                <UserControls:LegendItem Visibility="{Binding Path=IsDirtyVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FreqReserve:OverView}}, Converter={StaticResource btvc}}" Identifier="Pink" Text="Ikke sendt"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="Yellow" Text="Sendt"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="LightGreen" Text="Accepteret"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="White" Text="Ikke accepteret"></UserControls:LegendItem>
            </UserControls:LegendControl.Items>
        </UserControls:LegendControl>
    </DockPanel>
</UserControl>
<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView"
             ...
             xmlns:local="clr-namespace:PBA.Application.Client.UserControls.UserControls">

    ...
       <DockPanel>
        <UserControls:LegendControl>
            <UserControls:LegendControl.Items>
                <UserControls:LegendItem IsVisible="{Binding Path=IsDirtyVisible, 
                    RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type local:FreqReserve.OverView}}}" 
                    Identifier="Pink" 
                    Text="Ikke sendt"></UserControls:LegendItem>

               ....         

        </UserControls:LegendControl>
    </DockPanel>
</UserControl>

...
....         

请注意,您必须为“local”声明设置正确的名称空间,但如果您不确定名称空间应该是什么,则应该从IntelliSense获取该名称空间。

您的AncestorType中有一个输入错误。您想说FreqReserve.OverView。此外,还必须引用UserControl中定义的库名称空间

大概是这样的:

<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView" xmlns:FreqReserve="clr-namespace:PBA.Application.Client.UserControls.UserControls.FreqReserve">
...
    <DockPanel>
        <UserControls:LegendControl>
            <UserControls:LegendControl.Items>
                <UserControls:LegendItem Visibility="{Binding Path=IsDirtyVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FreqReserve:OverView}}, Converter={StaticResource btvc}}" Identifier="Pink" Text="Ikke sendt"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="Yellow" Text="Sendt"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="LightGreen" Text="Accepteret"></UserControls:LegendItem>
                <UserControls:LegendItem Identifier="White" Text="Ikke accepteret"></UserControls:LegendItem>
            </UserControls:LegendControl.Items>
        </UserControls:LegendControl>
    </DockPanel>
</UserControl>
<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView"
             ...
             xmlns:local="clr-namespace:PBA.Application.Client.UserControls.UserControls">

    ...
       <DockPanel>
        <UserControls:LegendControl>
            <UserControls:LegendControl.Items>
                <UserControls:LegendItem IsVisible="{Binding Path=IsDirtyVisible, 
                    RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type local:FreqReserve.OverView}}}" 
                    Identifier="Pink" 
                    Text="Ikke sendt"></UserControls:LegendItem>

               ....         

        </UserControls:LegendControl>
    </DockPanel>
</UserControl>

...
....         

请注意,您必须为“local”声明设置正确的名称空间,但如果您不确定名称空间应该是什么,则应该从IntelliSense获取该名称空间。

抱歉。在去除绒毛时走得有点太远,手工编写了一点代码,因为它不再存在于实际代码中。代码段已更新,以更好地反映实际代码。让我们从简单开始,并找出解决方法。IsDirtyVisible是概览控件的属性吗?是的,它是一个包装的dependencyproperty。该属性和DP是什么样子的?私有静态只读dependencyproperty_isDirtyVisibleProperty=dependencyproperty.Register(“IsDirtyVisible”、typeof(bool)、typeof(OverView)、new PropertyMetadata(true));public bool IsDirtyVisible{get{return(bool)GetValue(_isDirtyVisibleProperty);}set{SetValue(_isDirtyVisibleProperty,value);}}对不起。在去除绒毛时走得有点太远,手工编写了一点代码,因为它不再存在于实际代码中。代码段已更新,以更好地反映实际代码。让我们从简单开始,并找出解决方法。IsDirtyVisible是概览控件的属性吗?是的,它是一个包装的dependencyproperty。该属性和DP是什么样子的?私有静态只读dependencyproperty_isDirtyVisibleProperty=dependencyproperty.Register(“IsDirtyVisible”、typeof(bool)、typeof(OverView)、new PropertyMetadata(true));public bool IsDirtyVisible{get{return(bool)GetValue(_isDirtyVisibleProperty);}set{SetValue(_isDirtyVisibleProperty,value);}