样式中绑定错误在绘制图像中。

5
<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 Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ContentControl', AncestorLevel='1''. BindingExpression:Path=Foreground; DataItem=null; target element is 'GeometryDrawing' (HashCode=8154127); target property is 'Brush' (type '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>

您有多个ContentControls,这可能会导致相对源绑定出现问题(即使它起作用),您是否尝试使用ElementName绑定? - Mike Eason
如果我使用 ElementName 绑定或删除其中一个 ContentControl,该错误也会发生。 - Jotrius
这很奇怪,我的意思是它确实可以工作,但是还是会抛出一个错误。虽然忽略它会让人心动,但肯定有一些原因导致它出错。事实上,我不知道为什么。 - Mike Eason
出于好奇,如果您通过“标记”属性而不是“前景色”传递颜色字符串会怎样?似乎存在颜色冲突。 - Chris W.
如果我使用ContentControl的Tag属性,也会出现错误。 - Jotrius
显示剩余2条评论
1个回答

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>

一种更加高级的绑定到父元素的方式:

   RelativeSource={RelativeSource TemplatedParent}

编辑:

如果您想要抑制这些VS错误,请访问链接


谢谢你的回答,但是如果我为GeometryDrawing设置x:Name或者使用RelativeSource={RelativeSource TemplatedParent},绑定错误并没有消失。 - Jotrius
如果您同时使用RelativeSource TemplatedParent和x:Name,至少我在输出中不会收到此错误。只需尝试将我的答案复制粘贴到您的解决方案中即可。 - Dragos Stoica
好的,它可以工作,但只适用于我问题中的第二个代码示例。我先只用第一个代码示例进行测试。顺便说一下,在这种情况下,两种绑定变体都可以使用:FindAncestor和TemplatedParent。 - Jotrius
我也会先调查第一个例子。 - Dragos Stoica
我无法找到第一个示例的解决方案,因为Snoop不会生成任何错误。因此,没有绑定错误。如果您想要抑制这些错误消息,请访问此链接:http://www.codeproject.com/Tips/124556/How-to-suppress-the-System-Windows-Data-Error-warn - Dragos Stoica

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接