在DataTemplates内部绑定时出现“找不到控制FrameworkElement...”警告

18

我在Visual Studio的输出窗口中绑定一个SolidColorBrush属性时,在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')

如果我直接在矩形元素上进行绑定,而不是在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>

我的ViewModel(TestViewModel):

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

        public TestViewModel() {

        }
  }

更新:
显然与为SolidColorBrush绑定Color属性有关。如果我在一个RotateTransform对象上绑定Angle属性,同样的事情也会发生。

提前感谢。

1个回答

15

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

你需要通过一个转换器将颜色转换为实心画刷,并将其与背景填充绑定。

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

或者使用颜色的 DynamicResource,并在实心颜色刷中引用它。

ControlTemplate Storyboard颜色动画问题


1
感谢您的回答AngelWPF。在网上寻找解决方案时,我已经找到了这两个解决方案(例如这里)。实际上,我想知道的是为什么。您说默认上下文上的数据绑定对SolidColorBrush不起作用,因为它不是FrameworkElement。那么,为什么直接绑定而不通过DataTemplate传递时它可以工作呢?即使那样,它也可以正常工作,但会触发令人讨厌的警告。这可能只是我应该忽略的WPF错误吗? - Ioan Bucur
2
这不太对 - SolidColourBrush 可能不是一个 FrameworkElement,但它是一个 DependencyObject,而 Color 是一个 DependencyProperty。对它的绑定工作正常 - 直到你在没有传递 DataContext 的上下文中。为了我想要的,我需要在样式中的 Binding 中使用 DataContext(基于其他颜色构建 SolidColorBrush),然后遇到了这个问题(无法解决;必须重新设计),但是当直接将新的 SolidColorBrush 分配给 <Grid.Background> 时,我可以很愉快地绑定到 Color。 - tobriand
使用转换器对我没有起作用。根据@tobriand的评论,我重新设计了绑定。我没有直接将颜色绑定到SolidColorBrush,而是在我的UserControl中创建了一个SolidColorBrush资源,并在那里应用了绑定。在我的情况下,我需要一个Brush来绘制GeometryDrawing,所以我将它分配为StaticResource资源。这消除了所有错误并正常工作。 - redcurry
我曾经遇到过类似的问题,不同之处在于它是与Style而非DataTemplate有关。警告仅针对Color绑定发出,其他绑定都没有问题。我的解决方案是将SolidColorBrushes(具有颜色绑定的那些)定义为资源,然后在Style中引用它们作为DynamicResource(而不是StaticResource)。 然而,这只部分起作用:在应用程序启动时,仍会有一个SolidColorBrushes的警告,但稍后在运行时,当使用该样式的元素添加到可视树中时,警告就不再出现了。 - Tigerfink

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接