c# - DrawingImage 样式绑定(bind)错误

标签 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 的颜色。它工作正常。

但输出显示绑定(bind)错误:

System.Windows.Data 错误:4:找不到与引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.ContentControl',AncestorLevel='1'' 绑定(bind)的源。绑定(bind)表达式:路径=前景;数据项=空;目标元素是 'GeometryDrawing' (HashCode=8154127);目标属性是“画笔”(类型“画笔”)

为什么会出现这个错误?我该如何修复它或忽略它?

编辑

在这种情况下也会出现错误:

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

绑定(bind)到父级的更奇特的方式:

   RelativeSource={RelativeSource TemplatedParent}

编辑:

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

关于c# - DrawingImage 样式绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29123527/

相关文章:

c# - 在由多个 View 模型绑定(bind)的 View 中绑定(bind)图像不起作用

c# - WPF 每个 TabItem 的初始焦点

c# - 我有 5 个法规,我需要确保 3 个法规被 3 个不同的人占用

c# - entlib 6 记录器实例化错误

c# - monodroid - 默认新的未修改模板项目无法在 android 模拟器中运行

c# - System.Net.Http.HttpClient 缓存行为

wpf - 使 DatePicker 在禁用时更易于阅读

c# - 如何在 Xaml 值字段中设置自定义颜色值?

c# - 如何在 ComboBox 的开头和结尾显示 ComboBoxItem

WPf ListView : Save reorderd column order