我的代码未绑定到可观察集合中项目的X和Y属性。怎么了:
<ItemsControl ItemsSource="{Binding LED}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="SkyBlue"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Stroke="{Binding Color}" Fill="{Binding FillColor}" StrokeThickness="1" Width="40" Height="40"></Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
它确实绑定到Color和FillColor。这是Shape类,存储在ObservableCollection LED中:
class Shape
{
public int X { get; private set; }
public int Y { get; private set; }
public string Color { get; private set; }
public string FillColor { get; private set; }
public Shape (int x, int y, string color, string fillColor)
{
X = x;
Y = y;
Color = color;
FillColor = fillColor;
}
}
Setter.Value属性的文档有以下注意事项:
Windows Presentation Foundation(WPF)和Microsoft Silverlight支持使用Binding表达式为样式中的Setter提供值的功能。Windows运行时不支持Setter.Value的绑定用法(绑定不会评估,并且Setter无效,您不会收到错误,但也不会获得想要的结果)。从WPF或Silverlight XAML转换XAML样式时,请使用设置值的字符串或对象替换任何绑定表达式用法,或将这些值重构为共享的{StaticResource}标记扩展值,而不是绑定获得的值。
作为解决方法,您可以尝试使用RenderTransform代替:
<Ellipse Stroke="{Binding Color}" Fill="{Binding FillColor}" StrokeThickness="1" Width="40" Height="40">
<Ellipse.RenderTransform>
<TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
</Ellipse.RenderTransform>
</Ellipse>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句