问题描述
我有一个 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:
这是我的 ItemControl
Here is the definition of my ItemControl
:
<ItemsControl
Style="{StaticResource ItemsControlStyle}"
ItemsSource="{Binding Zones}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Style="{StaticResource RectangleStyle}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
以下是 ItemsControlStyle
的定义: / p>
Here is the definition of 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 setter of 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.
我可以如何修复绑定?
修改
我在MS Connect上提交了一个错误报告:
I submitted a bug report on MS Connect:
推荐答案
p>我也不能解释错误信息的原因,但是我发现添加一个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风格导致莫名的“找不到管理框架元素”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!