在DataTemplates中进行绑定时出现“找不到控制性FrameworkElement ...”警告 [英] 'Cannot find governing FrameworkElement...' warning when binding inside DataTemplates

查看:76
本文介绍了在DataTemplates中进行绑定时出现“找不到控制性FrameworkElement ...”警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当绑定到DataTemplate内的SolidColorBrush属性 时,我在Visual Studio输出窗口中收到此警告:

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


System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path = MyColor; DataItem = null;目标元素是 SolidColorBrush(HashCode = 22943289);目标属性为颜色(类型为颜色)

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')

如果我直接绑定到矩形元素上,则在DataTemplate外部 ,一切正常。

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:

我的视图:

<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>

我的ViewModel(TestViewModel):

My ViewModel (TestViewModel):

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

        public TestViewModel() {

        }
  }

更新:

显然与绑定 SolidColorBrush Color 属性有关。如果我将 Angle 属性绑定到 RotateTransform 对象上,就会发生同样的事情。

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.

预先感谢。 / p>

Thanks in advance.

推荐答案

使用默认数据源绑定为 DataContext 不适用于 SolidColorBrush 类型,因为它们不是框架元素。另外,它们是可冻结的,并且不允许您通过基于数据上下文的颜色绑定来动态更改其颜色。

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.

要么您必须通过a将颜色绑定到背景填充

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}}" />

或将Color用作 DynamicResource 并引用

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

ControlTemplate故事板彩色动画问题

这篇关于在DataTemplates中进行绑定时出现“找不到控制性FrameworkElement ...”警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆