【问题标题】:'Cannot find governing FrameworkElement...' warning when binding inside DataTemplates在 DataTemplates 中绑定时出现“找不到管理 FrameworkElement ...”警告
【发布时间】:2011-12-17 02:14:08
【问题描述】:

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

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=我的颜色;数据项=空;目标元素是“SolidColorBrush”(HashCode=22943289);目标属性是“颜色”(输入“颜色”)

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

谁能从下面的示例代码中解释为什么这两种明显相似的用法会出现这种差异:

我的观点:

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

我的视图模型(TestViewModel):

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

        public TestViewModel() {

        }
  }

更新:
这显然与绑定 SolidColorBrushColor 属性有关。如果我将 Angle 属性绑定到 RotateTransform 对象上,也会发生同样的事情。

提前致谢。

【问题讨论】:

    标签: wpf warnings datatemplate


    【解决方案1】:

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

    您必须通过将颜色转换为纯色画笔的转换器将颜色绑定到背景填充。

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

    或者使用颜色作为DynamicResource 并在纯色画笔中引用。

    ControlTemplate Storyboard color animation problem

    【讨论】:

    • 感谢您的回答 AngelWPF。在网上寻找解决方案我已经找到了这两个解决方案(例如here)。其实我想知道的是为什么。您说默认上下文上的数据绑定在 SolidColorBrush 上不起作用,因为它不是 FrameworkElement。那为什么不通过DataTemplate直接绑定就可以了。即使那样,它也可以正常工作,但会发出令人讨厌的警告。难道这只是一个我应该忽略的 WPF 错误?
    • 这不太对 - SolidColourBrush 可能不是FrameworkElement,但它是DependencyObject,而ColorDependencyProperty。其上的绑定工作正常 - 直到您处于未传递 DataContext 的上下文中。对于我想要的,我需要 Binding 内的 DataContext (基于其他颜色构建 SolidColorBrush),并遇到了这个问题(无法解决;必须重新设计),但能够在将新的SolidColorBrush 直接分配给&lt;Grid.Background&gt; 时,非常高兴地绑定到颜色。
    • 使用转换器对我不起作用。根据@tobriand 的评论,我重新设计了绑定。我没有将 Color 直接绑定到 SolidColorBrush,而是在我的 UserControl 中创建了 SolidColorBrush 资源并在那里应用了绑定。就我而言,我需要一个用于 GeometryDrawing 的画笔,因此我将资源分配为 StaticResource。这消除了所有错误并正常工作。
    猜你喜欢
    • 2020-02-10
    • 2013-08-17
    • 1970-01-01
    • 2013-03-24
    • 2014-02-03
    • 2011-03-12
    • 1970-01-01
    • 2020-05-14
    • 2021-08-22
    相关资源
    最近更新 更多