none
Cannot find governing FrameworkElement or FrameworkContentElement for target element

    Question

  • Hi

    I'm having a problem with the following control template

    <Style TargetType="{x:Type local:SymbolFeature}" > 
        <Setter Property="Template">  
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type local:SymbolFeature}">  
                    <Border x:Name="border">  
                        <Ellipse Fill="{TemplateBinding Foreground}" StrokeThickness="1" Stroke="Black" Width="40" Height="20" RenderTransformOrigin="0.5, 0.5">  
                            <Ellipse.RenderTransform> 
                                <TransformGroup> 
                                    <TranslateTransform X="{Binding OffsetX, RelativeSource={RelativeSource TemplatedParent}}" 
                                                        Y="{Binding OffsetY, RelativeSource={RelativeSource TemplatedParent}}" /> 
                                    <RotateTransform Angle="{Binding Angle, RelativeSource={RelativeSource TemplatedParent}}" /> 
                                </TransformGroup> 
                            </Ellipse.RenderTransform> 
                        </Ellipse> 
                    </Border> 
                    <ControlTemplate.Triggers> 
                        <Trigger Property="Visible" Value="False">  
                            <Setter Property="Visibility" Value="Collapsed" /> 
                        </Trigger> 
                    </ControlTemplate.Triggers> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 

    In the debugger console, I receive the following debug error:

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

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

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

    SymbolFeature derives from Control, and Angle, OffsetX and OffsetY are implemented as Dependency Properties.

    There will be plenty of instances of this control in a window, and I worry the error will result in performance costs. I am also the type who hate to see compiler/debugger warnings, as I believe they are warnings for a reason.

    How can I get rid of this debugger error? I was hoping I could use TemplateBindings, however, there seems to be some poblems with Inheritance context for the Transforms.

    Regards,
    Bjørnar
    Monday, November 17, 2008 7:32 PM

Answers

  • -> I hope someone from Microsoft can tell us why this is happening and if it really can lead to a performance problem.

    I think the reason why the data binding is created twice is basically because when you instantiate the Style instance from XAML, and an optimized template content should be generated, at this time, a binding will be instantiated which will fail activation and evaluation because at this time, there is not valid templated parent.

    At the second time, when the template is applied to the custom control, and it's expanded, the sub visual tree needs to be generated based on the optimized template content, at this time, the same binding will be generated/attached/activated again which succeeds the activation and evaluation.

    Hope this makes sense to you.
    • Marked as answer by Marco Zhou Monday, November 24, 2008 10:21 AM
    Thursday, November 20, 2008 9:58 AM

All replies

  • Have some debug info that's too strange for words :-(

    When I use this style the errors disappear:
    <Style TargetType="{x:Type local:SymbolFeature}">  
        <Setter Property="Template">  
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type local:SymbolFeature}">  
                    <Border x:Name="border">  
                        <StackPanel> 
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <Ellipse Fill="{TemplateBinding Foreground}" StrokeThickness="1" Stroke="Black" Width="40" Height="20" RenderTransformOrigin="0.5, 0.5">  
                                <Ellipse.RenderTransform> 
                                    <TransformGroup> 
                                        <TranslateTransform X="{Binding OffsetX, RelativeSource={RelativeSource TemplatedParent}}"    
                                                            Y="{Binding OffsetY, RelativeSource={RelativeSource TemplatedParent}}" /> 
                                        <RotateTransform Angle="{Binding Angle, RelativeSource={RelativeSource TemplatedParent}}" /> 
                                    </TransformGroup> 
                                </Ellipse.RenderTransform> 
                            </Ellipse> 
                        </StackPanel> 
                    </Border> 
                    <ControlTemplate.Triggers> 
                        <Trigger Property="Visible" Value="False">  
                            <Setter Property="Visibility" Value="Collapsed" /> 
                        </Trigger> 
                    </ControlTemplate.Triggers> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 
     

    If I remove one (1) TextBox the errors are back!?!?

    If I add a binding trace statement like below I can remove 3 of the TextBoxes without errors:
    <Style TargetType="{x:Type local:SymbolFeature}">  
        <Setter Property="Template">  
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type local:SymbolFeature}">  
                    <Border x:Name="border">  
                        <StackPanel> 
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <!--<TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>  
                            <TextBox Text="Test" Width="40" Height="20"/>--> 
                            <Ellipse Fill="{TemplateBinding Foreground}" StrokeThickness="1" Stroke="Black" Width="40" Height="20" RenderTransformOrigin="0.5, 0.5">  
                                <Ellipse.RenderTransform> 
                                    <TransformGroup> 
                                        <TranslateTransform X="{Binding OffsetX, RelativeSource={RelativeSource TemplatedParent}}"    
                                                            Y="{Binding OffsetY, RelativeSource={RelativeSource TemplatedParent}}" /> 
                                        <RotateTransform Angle="{Binding Angle, RelativeSource={RelativeSource TemplatedParent}, diagnostics:PresentationTraceSources.TraceLevel=High}" /> 
                                    </TransformGroup> 
                                </Ellipse.RenderTransform> 
                            </Ellipse> 
                        </StackPanel> 
                    </Border> 
                    <ControlTemplate.Triggers> 
                        <Trigger Property="Visible" Value="False">  
                            <Setter Property="Visibility" Value="Collapsed" /> 
                        </Trigger> 
                    </ControlTemplate.Triggers> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 

    If I remove one more TextBox the errors are back!

    Sorry, can't explain it!

    Marcel
    • Edited by DutchMarcel Thursday, November 20, 2008 11:58 AM changed 'remove 4' to 'remove 3'
    Tuesday, November 18, 2008 10:08 AM
  • If I look the trace info for the Angle binding in my last example, it seems there are 2 binding expressions created for the same Path.
    The first expression seems to pause, when the second one is created. The second one succeeds, but then the first one fails with the error that it can't find the source.
    See the output below and notice the two different hashes for the binding expressions:
    System.Windows.Data Warning: 52 : Created BindingExpression (hash=10261382) for Binding (hash=22597652)  
    System.Windows.Data Warning: 54 :   Path: 'Angle'  
    System.Windows.Data Warning: 56 : BindingExpression (hash=10261382): Default mode resolved to OneWay  
    System.Windows.Data Warning: 57 : BindingExpression (hash=10261382): Default update trigger resolved to PropertyChanged  
    System.Windows.Data Warning: 58 : BindingExpression (hash=10261382): Attach to System.Windows.Media.RotateTransform.Angle (hash=59109011)  
    System.Windows.Data Warning: 60 : BindingExpression (hash=10261382): Use Framework mentor <null> 
    System.Windows.Data Warning: 63 : BindingExpression (hash=10261382): Resolving source   
    System.Windows.Data Warning: 65 : BindingExpression (hash=10261382): Framework mentor not found  
    System.Windows.Data Warning: 61 : BindingExpression (hash=10261382): Resolve source deferred  
    System.Windows.Data Warning: 91 : BindingExpression (hash=10261382): Got InheritanceContextChanged event from RotateTransform (hash=59109011)  
    System.Windows.Data Warning: 63 : BindingExpression (hash=10261382): Resolving source   
    System.Windows.Data Warning: 65 : BindingExpression (hash=10261382): Framework mentor not found  
    'TemplateBindingTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\PresentationFramework.Luna\3.0.0.0__31bf3856ad364e35\PresentationFramework.Luna.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.  
    System.Windows.Data Warning: 52 : Created BindingExpression (hash=38493088) for Binding (hash=64981649)  
    System.Windows.Data Warning: 54 :   Path: 'Angle'  
    System.Windows.Data Warning: 56 : BindingExpression (hash=38493088): Default mode resolved to OneWay  
    System.Windows.Data Warning: 57 : BindingExpression (hash=38493088): Default update trigger resolved to PropertyChanged  
    System.Windows.Data Warning: 58 : BindingExpression (hash=38493088): Attach to System.Windows.Media.RotateTransform.Angle (hash=54467399)  
    System.Windows.Data Warning: 60 : BindingExpression (hash=38493088): Use Framework mentor <null> 
    System.Windows.Data Warning: 63 : BindingExpression (hash=38493088): Resolving source   
    System.Windows.Data Warning: 65 : BindingExpression (hash=38493088): Framework mentor not found  
    System.Windows.Data Warning: 61 : BindingExpression (hash=38493088): Resolve source deferred  
    System.Windows.Data Warning: 91 : BindingExpression (hash=38493088): Got InheritanceContextChanged event from RotateTransform (hash=54467399)  
    System.Windows.Data Warning: 63 : BindingExpression (hash=38493088): Resolving source   
    System.Windows.Data Warning: 65 : BindingExpression (hash=38493088): Framework mentor not found  
    System.Windows.Data Warning: 91 : BindingExpression (hash=38493088): Got InheritanceContextChanged event from RotateTransform (hash=54467399)  
    System.Windows.Data Warning: 63 : BindingExpression (hash=38493088): Resolving source   
    System.Windows.Data Warning: 66 : BindingExpression (hash=38493088): Found data context element: <null> (OK)  
    System.Windows.Data Warning: 68 :   RelativeSource.TemplatedParent found SymbolFeature (hash=35113868)  
    System.Windows.Data Warning: 74 : BindingExpression (hash=38493088): Activate with root item SymbolFeature (hash=35113868)  
    System.Windows.Data Warning: 104 : BindingExpression (hash=38493088):   At level 0 - for SymbolFeature.Angle found accessor DependencyProperty(Angle)  
    System.Windows.Data Warning: 100 : BindingExpression (hash=38493088): Replace item at level 0 with SymbolFeature (hash=35113868), using accessor DependencyProperty(Angle)  
    System.Windows.Data Warning: 97 : BindingExpression (hash=38493088): GetValue at level 0 from SymbolFeature (hash=35113868) using DependencyProperty(Angle): '45'  
    System.Windows.Data Warning: 76 : BindingExpression (hash=38493088): TransferValue - got raw value '45'  
    System.Windows.Data Warning: 85 : BindingExpression (hash=38493088): TransferValue - using final value '45'  
    System.Windows.Data Warning: 63 : BindingExpression (hash=10261382): Resolving source   
    System.Windows.Data Warning: 65 : BindingExpression (hash=10261382): Framework mentor not found  
    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=OffsetXDataItem=null; target element is 'TranslateTransform' (HashCode=52697953); target property is 'X' (type 'Double')  
    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=OffsetYDataItem=null; target element is 'TranslateTransform' (HashCode=52697953); target property is 'Y' (type 'Double')  
    System.Windows.Data Warning: 63 : BindingExpression (hash=10261382): Resolving source  (last chance)  
    System.Windows.Data Warning: 65 : BindingExpression (hash=10261382): Framework mentor not found  
    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AngleDataItem=null; target element is 'RotateTransform' (HashCode=59109011); target property is 'Angle' (type 'Double')  
     
     

    I hope someone from Microsoft can tell us why this is happening and if it really can lead to a performance problem.

    Marcel

    PS: You need this xml namespace to use the diagnostics: xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    • Edited by DutchMarcel Tuesday, November 18, 2008 11:40 AM added PS
    Tuesday, November 18, 2008 10:19 AM
  • -> I hope someone from Microsoft can tell us why this is happening and if it really can lead to a performance problem.

    I think the reason why the data binding is created twice is basically because when you instantiate the Style instance from XAML, and an optimized template content should be generated, at this time, a binding will be instantiated which will fail activation and evaluation because at this time, there is not valid templated parent.

    At the second time, when the template is applied to the custom control, and it's expanded, the sub visual tree needs to be generated based on the optimized template content, at this time, the same binding will be generated/attached/activated again which succeeds the activation and evaluation.

    Hope this makes sense to you.
    • Marked as answer by Marco Zhou Monday, November 24, 2008 10:21 AM
    Thursday, November 20, 2008 9:58 AM
  • Hi Marco,

    I think I can live with that explanation. I tested it and an output message in Window1.OnApplyTemplate() falls right in between the creation of the two binding expressions.
    But could this become a performance issue when a lot of these objects are created on e.g. a canvas or in a list?

    What I was really hoping for was an explanation of the strange behavior I noticed in my first post. :-)
    It seems that the mere presence of enough TextBoxes was enough to get the first binding nicely detached, instead of giving a data error.
    And I did some more testing and it seems I was lucky to pick a TextBox (originally to debug some values). I tested with ContentControl, Button, Thumb, Rectangle, TextBlock and RichTextBox to cover several classes in the inheritance tree.
    Besides the TextBox, only the presence of just a single RichTextBox also had the first binding nicely detached, instead of giving a data error. What's so special about them (or TextBoxBase?). :-)

    You can download my test project from here:
    http://cid-027cdb908d14e584.skydrive.live.com/self.aspx/WPFSamples/TemplateBindingTest.zip

    Marcel
    • Edited by DutchMarcel Thursday, November 20, 2008 2:25 PM Added link to test project
    Thursday, November 20, 2008 12:48 PM
  • Marco

    > Hope this makes sense to you.

    Right now, I don't understand it. I will have to read it again when I am actually awake. Is there a way to work around this error? Also, will this error cause any performance penalties (as I will be using a heap of this object on a canvas)?

    As it keeps evaluating a few times, I expect it to be costly. I initially tried to bind the transforms to a TemplateBinding, but that resulted in no values being bound. From other posts you have replied to, this is because TemplateBindings don't support Inheritance contexts, which I expect still holds true?
    Thursday, November 20, 2008 9:12 PM
  • Marcel,

    I tried your test project, but I still got the Binding error no matter how many/few TextBoxes I used.

    PS: Thank you for staying on the case :)
    Thursday, November 20, 2008 9:17 PM
  • To Marcel:

    -> But could this become a performance issue when a lot of these objects are created on e.g. a canvas or in a list?

    I don't think there will be a noticeable performance hit in particular, XAML generated template will only be instantiated once and shared by all templated parent ( it can also be reused across threading boundaries because template is designed to be shareable), in other words, a single template could be used on multiple templated parent (this is called template expansion).

    -> It seems that the mere presence of enough TextBoxes was enough to get the first binding nicely detached, instead of giving a data error.

    I can reproduce this issue, and it's really interesting that this behavior is so "inconsistent". The BAML parser has done a lot of interesting but bizzard optimization for generating and expanding template, which makes the code hard to read, so I have to say I've no idea what happened, if you are interested in what happens behind this, I could invite some one from the XAML parser team for idea.

    To Bjørnar:

    -> I tried your test project, but I still got the Binding error no matter how many/few TextBoxes I used.

    I cannot reproduce this error you said in the original thread, aka "Cannot find governing FrameworkElement and FrameworkContentElement for target element" error, could you please provide a small, complete and ready-to-run example to demonstrate what you are experiencing?

    Thanks

    Friday, November 21, 2008 3:38 AM
  • Marco,

    I get the error "System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AngleDataItem=null; target element is 'RotateTransform' (HashCode=59109011); target property is 'Angle' (type 'Double')" from the template in my initial post, Marcels follow-up and for the test project from his link. I am not able to reproduce the the template without getting Error 2. Commenting out TextBoxes or adding more. Makes no difference. Get the exception in all instances.
    Friday, November 21, 2008 8:24 AM
  • Maybe a .Net or VS version difference? I'm running VS2008 SP1 on .Net 3.5 SP1 (WinXP).

    Marcel
    Friday, November 21, 2008 8:49 AM
  • Home: VS2008 SP1 + .NET 3.5 SP1 (Vista)
    Work: VS2008 SP1 + .NET 3.5 SP1 (WinXP)

    At home, I get Error 2 with 0-7 TextBox elements in the template. On my work machine, I get the error with 0-5 TextBox elements. With 6 or more TextBoxes, I don't see the error.
    Friday, November 21, 2008 9:42 AM
  • I’m also having this issue. I have custom object with a default style in the Themes\Generic.xaml file. The custom object has a property Element and the control template defined in the default style has a visual brush that is supposed to display that Element inside the custom object. My custom object is actually a button to select a page and the page preview is displayed on the button, like in the toolbar on the left-hand side of PowerPoint.

        <Style BasedOn="{StaticResource RibbonButtonStyle}" TargetType="{x:Type local:cedPageSelector}">

            <Setter Property="Template">

                <Setter.Value>

                    <ControlTemplate>

                        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"

                                BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"

                                ToolTip="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ElementName}">

                            <Grid>

                                <Grid.ColumnDefinitions>

                                    <ColumnDefinition Width="Auto" />

                                    <ColumnDefinition Width="*" />

                                </Grid.ColumnDefinitions>

                                <TextBlock FontWeight="Bold" Foreground="{TemplateBinding Foreground}" Margin="5"

                                        Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PageNumber}" />

                                <Viewbox Grid.Column="1" Margin="0,5,5,5">

                                    <Viewbox MaxHeight="200" Width="200">

                                        <Rectangle

                                                Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Element.ActualHeight}"

                                                Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Element.ActualWidth}"

                                                Stroke="{StaticResource ControlBorderBrush}">

                                            <Rectangle.Fill>

                                                <VisualBrush

                                                        Visual="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Element" />

                                            </Rectangle.Fill>

                                        </Rectangle>

                                    </Viewbox>

                                </Viewbox>

                            </Grid>

                        </Border>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

        </Style>

     

    Everything works fine except for the cat that the following error appears in the immediate window:

    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Element; DataItem=null; target element is 'VisualBrush' (HashCode=27496772); target property is 'Visual' (type 'Visual')

     

    I hoped I could solve this with a value converter that checks whether the value is null and returns some valid visual if that is the case, like this:

        Public Overrides Function Convert(value As Object, targetType As Type, parameter As Object, _

        culture As CultureInfo) As Object

            If value Is Nothing Then

                Return New UIElement()

            Else

                Return value

            End If

        End Function

     

    However, the convert function is never called with a null value and the error remains. A way of suppressing the error would be handy. Keeping my immediate window tidy helps me detecting real errors easier.

    Regards,


    Rutger Koperdraad.
    Friday, October 28, 2011 2:54 PM
  • Actually, I found a good solution on Alan Tang’s blog: http://alantangcs.com/blog/?p=14. Instead of binding the element to the Visual property of the visual brush, I bind it directly to the Fill property of the rectangle and use a converter to convert the element into a visual brush. The errors disappear and everything keeps working as good as before.

    The XAML changes to:

    <Rectangle

    Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Element, Converter={StaticResource VisualBrushConverter}}"

    Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Element.ActualHeight}"

    Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Element.ActualWidth}"

                  Stroke="{StaticResource ControlBorderBrush}" />

     

    The converter uses this function:

     

        Public Overrides Function Convert(value As Object, targetType As Type, _

    parameter As Object, culture As CultureInfo) As Object

            Dim objVisual As Visual = TryCast(value, Visual)

            Dim objBrush As New VisualBrush With {.Visual = objVisual}

     

            Return objBrush

        End Function

     

    Hope this helps for other situations too.


    Rutger Koperdraad.
    Friday, October 28, 2011 3:31 PM