26

I have an ItemsControl that displays a bunch of rectangles. Each rectangle needs to be offset upward and to the left. So, I created a RectangleStyle that uses bindings to set the width, height, X translation, and Y translation for a rectangle.

The width and height bindings are working fine, but I'm getting the following error for the TranslateTransform bindings:

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

Here is the definition of my ItemControl:

<ItemsControl
    Style="{StaticResource ItemsControlStyle}"
    ItemsSource="{Binding Zones}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Style="{StaticResource RectangleStyle}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Here is the definition of ItemsControlStyle:

<Style x:Key="ItemsControlStyle" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding Point.X}" />
                <Setter Property="Canvas.Top" Value="{Binding Point.Y}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

And here is the definition of RectangleStyle:

<Style x:Key="RectangleStyle" TargetType="Rectangle">
    <Setter Property="Width" Value="{Binding Size.Width}" />
    <Setter Property="Height" Value="{Binding Size.Height}" />
    <Setter Property="RenderTransform">
        <Setter.Value>
            <!-- these bindings are causing the error -->
            <TranslateTransform X="{Binding Offset.X}" Y="{Binding Offset.Y}" />
        </Setter.Value>
    </Setter>
</Style>

The two bindings in the RenderTransform setter of RectangleStyle are the cause of the error, but I'm not sure what to do to fix the problem. Interestingly, the graphics are being translated properly, so WPF is able to resolve the bindings--it's just not happy about them for some reason.

What can I do to fix the bindings?


Edit

I submitted a bug report on MS Connect:

https://connect.microsoft.com/VisualStudio/feedback/details/746840/misleading-cannot-find-governing-frameworkelement-error-message-appears-in-output-window

devuxer
  • 41,681
  • 47
  • 180
  • 292
  • Well, it's easy to duplicate. I tried about 10 different things, but with the bindings working OK I'm left wondering if you ought to log this one on MS Connect and let them wonder if it's a bug. – Rob Perkins Jul 06 '10 at 23:03
  • Looks like the bug report link is either changed or no longer public. Wondering about the resolution of this since adding x:Name doesn't do anything for me. – Berin Loritsch Sep 28 '15 at 18:15

4 Answers4

58

I also cannot explain why the error message happens, but I have found out that adding an x:Name property to the transform is a way to get rid of the error message:

<TranslateTransform x:Name="myTransform" X="{Binding Offset.X}" Y="{Binding Offset.Y}" /> 
  • I have the same Problem when i use GradientStops in my Control Template Trigger. Giving a x:Name isn´t working for me. This Bug is really frustrating because i have around 20 GradientStops for one Control and in my View i use this Control 30 Times. Now you can image how long it takes to open the View while Debugging... – Bulli Jun 12 '13 at 06:15
  • 1
    Nice workaround; those spurious errors were really annoying me. Given how many years it has been since the original post, I'm guessing this was decided to not be worth fixing. – Dan Bryant May 09 '14 at 14:53
  • 1
    @Bulli You can find solution [here](http://stackoverflow.com/questions/3882767/binding-gradientstop-works-but-reports-error) – ghord Oct 27 '14 at 19:08
  • 6
    I had the same issue with ImageBrush, giving it a name fixed it. Thank you :) – adminSoftDK Aug 20 '15 at 14:06
  • 1
    Had the same error using .Net 3.5 and binding to ScaleTransform. Adding x:Name SEEMS to be a workaround. I can't verify that the binding error will never occure again. I also tried to debug the issue by using a DebugConverter but using the (or maybe any) converter seems to have the same effect as adding x:Name. So i wasn't able to debug anything because the error did not occure while using the converter. – MrToast Jul 07 '16 at 11:05
  • Adding x:Name worked for GeometryDrawing too (I had both data error 4 with RelativeSource FindaAncestor binding and data error 2 with regular binding). I'm using .NET Framework 4.5.2 and to think this is still happening... – Arie Jul 03 '17 at 09:26
  • I had the same problem with a TransformGroup of three transforms. Naming only one of the transforms, removed the error for all three. Naming the group did the same. Naming the parent element (in my case a Shape) instead, brought all errors back. Utterly weird... – oliver Apr 02 '18 at 20:21
  • 1
    The x:Name alters when elements are created, because it's checking they're unique. I think these spurious errors are due to a timing issue and I guess making the element and hence it's parent resolved early is why it works. – Andy Apr 26 '18 at 08:52
10

I think I found some useful info.

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/db050ce6-d084-41ad-9a31-c01831687683

The answer to this appears to be in the Microsoft explanation of the behavior as an ItemsControl goes through its compositing process and applies bindings and styles. That is, WPF is trying to optimize your DataTemplate before it has a source of data to successfully evaluate the bindings: "dataitem=null". In every other instance during its layout passes, "dataitem" points to something in your "Zones" IEnumerable and it's able to complete the bindings. Otherwise, you'd see the error with each item in your collection, rather than just once per property.

It appears to be a "pay no attention to the man behind the curtain" type of thing. And it should probably be added to MS Connect as a bug report; successful code shouldn't kick out "Error"s that don't matter. But I'll leave it to you to file this with MS Connect if you want.

Rob Perkins
  • 3,088
  • 1
  • 30
  • 51
  • 1
    +1, I agree, and I just submitted a bug on MS Connect: https://connect.microsoft.com/VisualStudio/feedback/details/746840/misleading-cannot-find-governing-frameworkelement-error-message-appears-in-output-window – devuxer Jun 06 '12 at 00:17
  • 3
    Wow. It took them something like five minutes to decide "gee whiz wow, yer right you betcha, but we're not gonna fix that." – Rob Perkins Jun 07 '12 at 18:17
  • 1
    Makes me wonder if WPF is already taking a back seat to WinRT. – devuxer Jun 07 '12 at 18:19
  • 1
    If you ask WinDiv, I think you'll get a very different answer than if you ask DevDiv. And of course the Phone people aren't really listening to WinDiv either, and DevDiv isn't listening to the WP people... But as far as certainty goes I think WPF has staying power as long as Visual Studio is based on it. – Rob Perkins Jun 07 '12 at 21:12
3

After reading Rob Perkins' answer, I added a FallbackValue to a binding with this issue. This cleared the error for me.

AdamRossWalker
  • 294
  • 1
  • 10
  • saved my life, thanks! It was not this simple for me though - simply specifying the fallback value in the binding did not work for me - I had to create the fallback value as a static resource and reference it in the binding. – G. B. Nov 24 '19 at 14:49
0

If this helps anyone else looking at this issue, even though the binding appears to work at runtime, this error in the Output window bugged me for ages. After much research, I discovered that a transform does not exist in the visual tree, therefore the binding cannot be resolved.

To solve this, move the Transform to be a child of your Rectangle instead of in your resource dictionary.

<Rectangle Style="{StaticResource RectangleStyle}">
    <Rectangle.RenderTransform>
        <TranslateTransform X="{Binding Offset.X}" Y="{Binding Offset.Y}" />
    </Rectangle.RenderTransform>
</Rectangle>