DataTemplates在WPF中显示Points作为省略号的集合

hel*_*ker 2 wpf datatemplate itemscontrol itemsource

在我的应用程序中,用户应该点击图像,然后点击一些点出现.他也可以通过右键单击等删除它们.

所以我有一个窗口(XAML代码隐藏+),用帆布组成的测试项目,而我处理一些事件,如MouseMoveMouseLeftButtonDown其打算把点添加到一个ObservableCollection<Point>在后面的代码.

我已经有了这个,我不知道我应该如何实现数据模板和数据绑定,以便我的网格将包含一个ItemsControl并且每个点都将显示为一个点(Path有一个EllipseGeometry,所以我可以设置它Center).

我看了一些教程,但大多数都有很多额外的代码,我很困惑.

Mik*_*bel 6

这是一个完全在XAML中实现的简单解决方案:

<!-- Bind ItemsSource to the appropriate collection -->
<ItemsControl ItemsSource="{Binding Points}">
  <ItemsControl.ItemContainerStyle>
    <Style TargetType="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.ItemsPanel>
    <ItemsPanelTemplate>
      <Canvas IsItemsHost="True" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)