1

I had a bunch of errors of the following type System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.

I was able to resolve most of those except the following two.

The first error is

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=WColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=34289570); target property is 'Color' (type 'Color')

The XAML code for this error is below

<ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=HasWColor}" Value="True">
            <Setter TargetName="HeaderIcon" Property="Fill">
                <Setter.Value>
                    <SolidColorBrush Color="{Binding Path=WColor, Converter={StaticResource ColorToBrushConverter}}"/>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </ControlTemplate>

I was able to resolve a very similar error in another file of my application by using the converter ColorToBrushConverter. The same converter doesn't work here though. Below is the code where this works.

<DataTemplate x:Key="GroupTemplate">
        <StackPanel x:Name="Group" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Border BorderThickness="0,2,0,3" BorderBrush="{DynamicResource TableBorderBrush}">
                <Border BorderThickness="7,0,0,0" >
                    <Border.BorderBrush>
                        <SolidColorBrush Color="{Binding GroupColor, Converter={StaticResource ColorToBrushConverter}}"/>
                    </Border.BorderBrush>
                </Border>
            </Border>
        </StackPanel>
    <DataTemplate/>

The second error is

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=TranslateY; DataItem=null; target element is 'TranslateTransform' (HashCode=10383263); target property is 'Y' (type 'Double')

The code for this error is

<MultiDataTrigger.Setters>
                    <Setter Property="Visibility" Value="Visible"/>
                    <Setter Property="RenderTransform">
                        <Setter.Value>
                            <TranslateTransform x:Name="myTransform">
                                <TranslateTransform.X>
                                    <MultiBinding Converter="{StaticResource OffsetConverter}">
                                        <Binding Path="DisplayedX"/>
                                        <Binding ElementName="LinesGrid" Path="ActualWidth"/>
                                        <Binding Source="17"/>
                                    </MultiBinding>
                                </TranslateTransform.X>
                                <TranslateTransform.Y>
                                    <MultiBinding Converter="{StaticResource OffsetConverter}">
                                        <Binding Path="TranslateY"/>
                                        <Binding ElementName="PointsGrid" Path="ActualHeight"/>
                                        <Binding Source="17"/>
                                        <Binding Path="PointType"/>
                                    </MultiBinding>
                                </TranslateTransform.Y>
                            </TranslateTransform>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger.Setters>

I have tried giving the TranslateTransform an x:Name property as suggested in this post but to no avail.

Any ideas on how to get rid of these errors?

Community
  • 1
  • 1
Tushar
  • 41
  • 2
  • 7
  • What type is HeaderIcon? – TrueEddie Jan 14 '14 at 21:42
  • If `ColorToBrushConverter` does what its name states - converts color->brush - then you shouldn't use this converter here, because `SolidColorBrush.Color` takes a color, not brush – SOReader Jan 14 '14 at 21:44
  • @TrueEddie: HeaderIcon is a rectangle. – Tushar Jan 14 '14 at 22:00
  • @SOReader: Using the converter is a hack I found on stack overflow. Here is one post using similar solution http://stackoverflow.com/questions/7926204/cannot-find-governing-frameworkelement-warning-when-binding-inside-datatemp – Tushar Jan 14 '14 at 22:02

1 Answers1

3

For the first one, if you want to use the converter hack, you should assign it to the Fill property directly, instead of creating a SolidColorBrush:

<Setter TargetName="HeaderIcon" Property="Fill" Value="{Binding Path=WColor, Converter={StaticResource ColorToBrushConverter}}"/>

For the second one, it's very hard to fix without more debugging information. Try adding a breakpoint to the converter, see if the error occurs before or after the breakpoint is hit.

robertos
  • 1,810
  • 10
  • 12
  • Your suggestion is for the code which works. You seemed to have misread which code is causing error and which is not. The error is caused by `Binding Path=WColor` – Tushar Jan 14 '14 at 23:01
  • I was able to resolve the first error by not using a `solidcolorbrush` and by using the `Fill` property directly. `` @robertos: Can you please edit your answer to reflect this. I can mark it as the correct answer then. I haven't been able to resolve the second error though. The converter handles both 3 and 4 parameters. I'm not sure how that would cause this error. – Tushar Jan 15 '14 at 17:24