dev*_*xer 22 data-binding wpf xaml binding
我有一个ItemsControl显示一堆矩形.每个矩形需要向上和向左偏移.所以,我创建了一个RectangleStyle使用绑定来设置矩形的宽度,高度,X平移和Y平移.
宽度和高度绑定工作正常,但我得到TranslateTransform绑定的以下错误:
System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径= Offset.X; 的DataItem = NULL; target元素是'TranslateTransform'(HashCode = 16452547); target属性为'X'(类型'Double')
这是我的定义ItemControl:
<ItemsControl
Style="{StaticResource ItemsControlStyle}"
ItemsSource="{Binding Zones}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Style="{StaticResource RectangleStyle}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
这是以下定义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>
Run Code Online (Sandbox Code Playgroud)
以下是定义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>
Run Code Online (Sandbox Code Playgroud)
RenderTransformsetter中的两个绑定RectangleStyle是错误的原因,但我不知道该怎么做才能解决问题.有趣的是,图形正在被正确翻译,因此WPF能够解析绑定 - 由于某些原因它们对它们不满意.
我该怎么做来修复绑定?
编辑
我提交了有关MS Connect的错误报告:
小智 49
我也无法解释为什么会出现错误消息,但我发现在转换中添加x:Name属性是一种摆脱错误消息的方法:
<TranslateTransform x:Name="myTransform" X="{Binding Offset.X}" Y="{Binding Offset.Y}" />
Run Code Online (Sandbox Code Playgroud)
我想我发现了一些有用的信息.
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/db050ce6-d084-41ad-9a31-c01831687683
这个问题的答案似乎是微软对这种行为的解释,因为ItemsControl经历了它的合成过程并应用了绑定和样式.也就是说,WPF正在尝试优化DataTemplate,然后才能拥有成功评估绑定的数据源:"dataitem = null".在其布局过程中的每个其他实例中,"dataitem"指向"Zones"IEnumerable中的某些内容,并且它能够完成绑定.否则,您会看到集合中每个项目的错误,而不是每个属性只有一次.
它似乎是一种"不关注窗帘背后的男人"的东西.它应该作为错误报告添加到MS Connect中; 成功的代码不应该踢出"无关紧要的错误".但是如果你愿意,我会留给你用MS Connect提交.
| 归档时间: |
|
| 查看次数: |
18956 次 |
| 最近记录: |