Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

wpf - 'Cannot find governing FrameworkElement...' warning when binding inside DataTemplates

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...