我有一个 ItemsControl,其中填充了在 DataTemplate 中定义并放置在 Canvas 上的项目。我希望能够在 Canvas 上的这些项目之一上使用右键单击事件,将其从包含的集合中删除,该集合位于我的 ViewModel 中。我一直在尝试使用 MVVM Light 的 EventToCommand 来找出一种方法,但到目前为止我一无所获。我不知道还能尝试什么,所以我来 SO 寻求帮助。任何意见,将不胜感激。
<DataTemplate DataType="{x:Type model:Player}">
<Canvas>
<Grid Canvas.Left="{Binding Location.X}"
Canvas.Top="{Binding Location.Y}"
MouseLeftButtonDown="Grid_MouseLeftButtonDown"
MouseLeftButtonUp="Grid_MouseLeftButtonUp"
MouseMove="Grid_MouseMove">
<Grid.LayoutTransform>
<RotateTransform Angle="-90" />
</Grid.LayoutTransform>
<Ellipse Width="12"
Height="12"
Fill="{Binding PrimaryColor}" />
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="6"
FontWeight="Bold"
Foreground="Black"
Text="{Binding Position.Abbreviation}" />
</Grid>
</Canvas>
</DataTemplate>
<Canvas x:Name="myCanvas"
Width="1440"
Height="640"
AllowDrop="True"
Background="Green"
Focusable="True">
<ContentPresenter Content="{Binding Field}" />
<ItemsControl ItemsSource="{Binding Team.Players}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas AllowDrop="True" Background="Transparent" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Canvas>