0

我有一个 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>
4

1 回答 1

1

您可以ContextMenu使用 Remove MenuItem。然后你就可以Command RemoveCommand在你的ViewModel. 您可以将MenuItem命令绑定到 VM 命令,如下所示,并在CommandParameter. 在命令处理程序中,您可以从集合中删除该项目。

<DataTemplate DataType="{x:Type model:Player}">
    <Canvas>               
        <Grid Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}} Canvas.Left="{Binding Location.X}"
              Canvas.Top="{Binding Location.Y}"
              MouseLeftButtonDown="Grid_MouseLeftButtonDown"
              MouseLeftButtonUp="Grid_MouseLeftButtonUp"
              MouseMove="Grid_MouseMove">
            <Grid.ContextMenu>
                <ContextMenu>
                        <MenuItem Header="Remove" 
                         Command="{Binding PlacementTarget.Tag.RemoveCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
                         CommandParameter="{Binding PlacementTarget.DataContext, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContextMenu}}}"/>
                    </ContextMenu>
            </Grid.ContextMenu>
            <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>
于 2013-10-18T18:36:22.267 回答