DrawingImage中样式的绑定错误

时间:2015-03-18 13:34:56

标签: c# wpf xaml data-binding imagesource

<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:无法找到带引用的绑定源&#39; RelativeSource FindAncestor,AncestorType =&#39; System.Windows.Controls.ContentControl&#39;,AncestorLevel =&# 39; 1&#39;&#39 ;. BindingExpression:路径=前景;的DataItem = NULL;目标元素是&#39; GeometryDrawing&#39; (的HashCode = 8154127);目标属性是&#39;刷&#39; (键入&#39;刷&#39;)

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

修改

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

<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>

1 个答案:

答案 0 :(得分:2)

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

  <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