在WPF风格的结合导致莫名的"找不到理事FrameworkElement的"错误 [英] Binding in WPF style causes inexplicable "Cannot find governing FrameworkElement" error
问题描述
我有一个的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上。 BindingEx pression:路径= Offset.X;的DataItem = NULL;目标元素是'TranslateTransform(哈希code = 16452547);目标属性是'X'(类型'双')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Offset.X; DataItem=null; target element is 'TranslateTransform' (HashCode=16452547); target property is 'X' (type '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>
二中绑定的的RenderTransform
二传手的RectangleStyle
都是错误的原因,但我不一定要做些什么来解决这个问题。有趣的是,显卡的是的被正确地翻译,所以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连接一个bug报告:
I submitted a bug report on MS Connect:
<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/details/746840/misleading-cannot-find-governing-frameworkelement-error-message-appears-in-output-window\">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风格的结合导致莫名的&QUOT;找不到理事FrameworkElement的&QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!