c# – DrawingImage中样式的绑定错误

<Window x:Class="Project.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="100" Height="400">

    <Window.Resources>
        <Style x:Key="IconStyle" TargetType="{x:Type Image}">
            <Setter Property="Source">
                <Setter.Value>
                    <DrawingImage>
                        <DrawingImage.Drawing>
                            <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=Foreground}">
                                <GeometryDrawing.Geometry>
                                    <PathGeometry Figures="M 0,0 0,10 10,5" />
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>

        <ContentControl Foreground="Red">
            <Image Style="{StaticResource IconStyle}" />
        </ContentControl>

        <ContentControl Foreground="Blue">
            <Image Style="{StaticResource IconStyle}" />
        </ContentControl>

    </StackPanel>

</Window>

此示例显示了两个图标.图标具有父ContentControl的颜色.它工作正常.

但输出显示绑定错误:

System.Windows.Data错误:4:无法找到绑定源,引用’RelativeSource FindAncestor,AncestorType =’System.Windows.Controls.ContentControl’,AncestorLevel =’1”. BindingExpression:路径=前景;的DataItem = NULL; target元素是’GeometryDrawing'(HashCode = 8154127); target属性是’Brush'(类型’Brush’)

为什么会出现此错误?我该如何解决或者可以忽略?

编辑

在这种情况下也会发生错误:

<Window x:Class="Project.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="100" Height="400">

    <Window.Resources>
        <Style x:Key="IconStyle" TargetType="{x:Type ContentControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ContentControl">
                        <Image>
                            <Image.Source>
                                <DrawingImage>
                                    <DrawingImage.Drawing>
                                        <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=Foreground}">
                                            <GeometryDrawing.Geometry>
                                                <PathGeometry Figures="M 0,0 0,10 10,5" />
                                            </GeometryDrawing.Geometry>
                                        </GeometryDrawing>
                                    </DrawingImage.Drawing>
                                </DrawingImage>
                            </Image.Source>
                        </Image>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>    
        <ContentControl Foreground="Red" Style="{StaticResource IconStyle}" />    
        <ContentControl Foreground="Blue" Style="{StaticResource IconStyle}" />    
    </StackPanel>

</Window>

最佳答案 只需设置一个名称,错误就会消失:

  <GeometryDrawing x:Name="GeometryDrawing" Brush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry Figures="M 0,0 0,10 10,5" />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>

绑定到Parent的更奇特的方式:

   RelativeSource={RelativeSource TemplatedParent}

编辑:

如果您想要抑制此VS错误,请访问link

点赞