我有一个ObservableCollection<Point>
显示为ItemsControl
,我在 XAML 中设置 和ItemsPanelTemplate
属性的地方。ItemsPanel
ItemTemplate
我希望我的点代表边界框内的标准化位置。所以,如果我有一个Point(0.5, 0.5)
,它将在容器的正中间。现在如果是Point(0.25, 0.75)
,它将定位在容器宽度的 25% 和容器高度的 75% 处。
问题是:我如何在 WPF 中实现它?我应该在 ItemContainerStyle 绑定中放置一个 ValueConverter 吗?我应该使用 Behavior、DataTemplate、ElementBinding、MultiBinding...吗?
我有点失落...
XAML 代码,使用绝对定位(不是我想要的):
<ItemsControl x:Name="MarcadoresSelecionadosZoom" ItemsSource="{Binding ListaPontos}" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="Point">
<Ellipse Fill="Blue"
Width="8"
Height="8"
Margin="-4,-4,4,4" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>