本文介绍了WPF风格的绑定导致莫名的“找不到治理FrameworkElement"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ItemsControl 显示一堆矩形.每个矩形都需要向上和向左偏移.因此,我创建了一个 RectangleStyle,它使用绑定来设置矩形的宽度、高度、X 平移和 Y 平移.

I have an ItemsControl that displays a bunch of rectangles. Each rectangle needs to be offset upward and to the left. So, I created a RectangleStyle that uses bindings to set the width, height, X translation, and Y translation for a rectangle.

宽度和高度绑定工作正常,但 TranslateTransform 绑定出现以下错误:

The width and height bindings are working fine, but I'm getting the following error for the TranslateTransform bindings:

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement.BindingExpression:Path=Offset.X;数据项=空;目标元素是TranslateTransform"(HashCode=16452547);目标属性是'X'(类型'Double')

这是我的ItemControl的定义:

<ItemsControl
    Style="{StaticResource ItemsControlStyle}"
    ItemsSource="{Binding Zones}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Style="{StaticResource RectangleStyle}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这里是ItemsControlStyle的定义:

<Style x:Key="ItemsControlStyle" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding Point.X}" />
                <Setter Property="Canvas.Top" Value="{Binding Point.Y}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

这里是RectangleStyle的定义:

<Style x:Key="RectangleStyle" TargetType="Rectangle">
    <Setter Property="Width" Value="{Binding Size.Width}" />
    <Setter Property="Height" Value="{Binding Size.Height}" />
    <Setter Property="RenderTransform">
        <Setter.Value>
            <!-- these bindings are causing the error -->
            <TranslateTransform X="{Binding Offset.X}" Y="{Binding Offset.Y}" />
        </Setter.Value>
    </Setter>
</Style>

RectangleStyleRenderTransform setter 中的两个绑定是导致错误的原因,但我不确定如何解决该问题.有趣的是,图形正在被正确翻译,因此 WPF 能够解决绑定问题——它只是出于某种原因对它们不满意.


The two bindings in the RenderTransform setter of RectangleStyle are the cause of the error, but I'm not sure what to do to fix the problem. Interestingly, the graphics are being translated properly, so WPF is able to resolve the bindings--it's just not happy about them for some reason.

我可以做些什么来修复绑定?

What can I do to fix the bindings?

编辑

我在 MS Connect 上提交了错误报告:

I submitted a bug report on MS Connect:

https://connect.microsoft.com/VisualStudio/feedback/details/746840/misleading-cannot-find-governing-frameworkelement-error-message-appears-in-output-window

推荐答案

我也无法解释为什么会出现错误消息,但我发现向转换添加 x:Name 属性是一种摆脱错误信息:

I also cannot explain why the error message happens, but I have found out that adding an x:Name property to the transform is a way to get rid of the error message:

<TranslateTransform x:Name="myTransform" X="{Binding Offset.X}" Y="{Binding Offset.Y}" /> 

这篇关于WPF风格的绑定导致莫名的“找不到治理FrameworkElement"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:49