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>

This example shows two icons. The icons have the color of the parent ContentControl. It works fine.

But the output shows a binding error:

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

Why does this error occur? How can I fix it or can I ignore?

EDIT

The error occurs also in this case:

<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>
Jotrius
  • 647
  • 4
  • 19
  • You have multiple ContentControls, this might cause a problem with your RelativeSource binding (even if it worked), have you tried using an ElementName binding instead? – Mike Eason Mar 18 '15 at 13:42
  • The error occurs also if I use ElementName binding or if I delete one of the two ContentControls. – Jotrius Mar 18 '15 at 13:50
  • It's pretty weird, I mean, it does work, but it throws an error anyway. It'd be tempting to ignore it, however there must be _some_ reason why it's throwing an error. The truth is, I don't know. – Mike Eason Mar 18 '15 at 14:00
  • Out of curiosity, what if you pass your color string through the `Tag` property instead of `Foreground` ? Seems like a color collision. – Chris W. Mar 18 '15 at 14:21
  • The error occurs also if I use the Tag property of the ContentControl. – Jotrius Mar 18 '15 at 15:19
  • Oh wait, I didn't notice it at first glance, pretty sure it's how you're using ContentControl amigo, and the TargetType, if it were me I'd do it like this [old answer](http://stackoverflow.com/questions/13292179/best-way-to-use-a-vector-image-in-wpf/13293017#13293017) instead and then you should lose that error. The example is SL but the concept is the same for wpf. – Chris W. Mar 18 '15 at 16:34
  • Hi thanks for your answer, but the error occurs also with your solution. (I have extended the question with your solution.) – Jotrius Mar 19 '15 at 09:26

1 Answers1

2

Just set a name and the error will disappear:

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

A more fancy way for binding to Parent:

   RelativeSource={RelativeSource TemplatedParent}

EDIT:

If you want to supress this VS errors, visit the link

Dragos Stoica
  • 1,753
  • 1
  • 21
  • 42
  • thanks for your answer, but the binding error doesn't disappear, if I set x:Name for the GeometryDrawing or if I use RelativeSource={RelativeSource TemplatedParent} – Jotrius Mar 19 '15 at 12:21
  • If you use both, RelativeSource TemplatedParent and x:Name will work, at least, i don`t receive this error in Output. Just try to copy-paste my answer in your solution – Dragos Stoica Mar 19 '15 at 12:27
  • Ok it works, but only for the **second code example** of my question. First I test it only with first code example. By the way, it works with both binding variants in this case: FindAncestor, TemplatedParent – Jotrius Mar 19 '15 at 12:41
  • I will investigate also first example. – Dragos Stoica Mar 19 '15 at 12:55
  • I cant find a solution for first example, because Snoop don`t generates any error. So, there is no binding error. If you want to supress this error messages please visit this : http://www.codeproject.com/Tips/124556/How-to-suppress-the-System-Windows-Data-Error-warn – Dragos Stoica Mar 19 '15 at 14:19