'Cannot find governing FrameworkElement...' warning when binding inside DataTemplates
Asked Answered
H

1

18

I'm getting this warning in Visual Studio output window when binding on a SolidColorBrush property inside a DataTemplate:

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

If I bind directly on the rectangle element, outside the DataTemplate, it all works well.

Can anyone explain why this difference in the two apparently similar usages from the sample code below:

My View:

<UserControl.Resources>

    <vm:TestViewModel x:Key="_myTestVM"/>

    <DataTemplate x:Key="testVMDataTemplate">
        <Grid>
            <Rectangle Height="30" Width="200" Margin="5">
                <Rectangle.Fill>
                    <SolidColorBrush Color="{Binding Path=MyColor}" />
                </Rectangle.Fill>
            </Rectangle>
        </Grid>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <StackPanel DataContext="{StaticResource _myTestVM}">
        <!-- Binding *outside* the DataTemplate = works fine -->
        <Rectangle Height="30" Width="200" Margin="5">
            <Rectangle.Fill>
                <SolidColorBrush Color="{Binding Path=MyColor}"/>
            </Rectangle.Fill>
        </Rectangle>

        <!-- Binding *inside* the DataTemplate = output warning -->    
        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource testVMDataTemplate}"/>
    </StackPanel>
</Grid>

My ViewModel (TestViewModel):

public class TestViewModel {
    private Color _color = Colors.Green;
        public Color MyColor {
            get { return _color; }
        }

        public TestViewModel() {

        }
  }

Update:
It evidently has to do with binding the Color property for the SolidColorBrush. The same thing is happening if I bind the Angle property on a RotateTransform object.

Thanks in advance.

Hardiman answered 28/10, 2011 at 7:13 Comment(0)
E
16

Binding with default data source as DataContext wont work for SolidColorBrush type as they are not framework elements. Plus they are freezable and you are not allowed to change their colors dynamically through data context based color binding.

Either you will have to bind the color to the background fill via a converter that converts the color into a solid color brush.

 <TextBlock Background="{Binding MyColor,
                                Converter={StaticResource ColorToBrushConverter}}" />

Or use Color as DynamicResource and refer that in Solid Color Brush.

ControlTemplate Storyboard color animation problem

Eastwards answered 28/10, 2011 at 7:37 Comment(4)
Thank you for your answer AngelWPF. Looking for a solution on the net I already found those two solutions(for example here). Actually what I wanted to know is why. You said data binding on the default context does not work on SolidColorBrush because it is not a FrameworkElement. Then why does it work when binding directly without passing by a DataTemplate. And even then, it works ok, but it fires that annoying warning. Could it be just a WPF bug I should ignore?Hardiman
This isn't quite right - SolidColourBrush may not be a FrameworkElement, but it is a DependencyObject, and Color is a DependencyProperty. Bindings on it work fine - until you're inside a context where DataContext isn't passed. For what I want I needed DataContext within a Binding (was building a SolidColorBrush based on some other color) within a style, and ran into this issue (couldn't workaround; had to redesign), but was able to bind to Color quite happily when assigning a new SolidColorBrush directly to <Grid.Background>.Puseyism
Using a Converter didn't work for me. Based on the comment by @tobriand, I redesigned the bindings. Instead of binding the Color directly to a SolidColorBrush, I created a SolidColorBrush resource in my UserControl and applied the binding there. In my case, I needed a Brush for a GeometryDrawing, so I assigned it the resource as a StaticResource. This eliminated all errors and worked properly.Electromagnetism
I had a similar problem with Style instead of DataTemplate. The warnings were only issued for bindings of Color, all other bindings were OK. My solution was to define SolidColorBrushes (the ones that had color bindings) as resources, and then reference them in the Style as DynamicResource (not StaticResource). However this only works partly: at application startup, there is still a warning for one of the two SolidColorBrushes, but later at run time when elements using the style are added to the visual tree, the warnings don't appear anymore.Nemhauser

© 2022 - 2024 — McMap. All rights reserved.