listview中的WPF KeyBinding

时间:2014-08-25 13:40:34

标签: wpf listview key-bindings

我有这段代码:

<Label Grid.Row="3" Grid.Column="0" Content="Content Version:" Style="{DynamicResource WorkspaceLabelsStyle}" />
    <ListView Validation.ErrorTemplate="{x:Null}" x:Name="VersionsList" Margin="0,5,5,0" Grid.Row="2" Grid.Column="2"  SelectedItem="{Binding Path=SelectedObject , Mode=TwoWay}"  ItemsSource="{Binding Path=SubItemVersionLinkNode}" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Height="150" Width="360" Grid.RowSpan="4" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding}" dd:DragDrop.UseDefaultDragAdorner="True">
        <ListView.InputBindings>
            <KeyBinding Gesture="Delete" Command="{Binding Path = DeleteSelectedVersionsCommand }"></KeyBinding>
                </ListView.InputBindings>
                <ListView.Resources>
                    <ContextMenu x:Key="ItemContextMenu">
                        <MenuItem Header="Open Linked Version" Command="{Binding Path=DataContext.OpenLinkedVersionInAnotherWorkspaceCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Background="WhiteSmoke" />
                        <MenuItem Header="Delete all" Command="{Binding Path=DataContext.DeleteAllVersionsCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Background="WhiteSmoke" />
                        <MenuItem Header="Delete selected" Command="{Binding Path=DataContext.DeleteSelectedVersionsCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Background="WhiteSmoke" />
                    </ContextMenu>
                    <Style TargetType="GridViewColumnHeader">
                        <Setter Property="IsEnabled" Value="False"/>
                        <Setter Property="Width" Value="120" />
                    </Style>
                </ListView.Resources>

                <ListView.View>
                    <GridView x:Name="SubItemVersionLinkNode" AllowsColumnReorder="False">
                        <GridViewColumn Header="Icon" Width="120">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Image Source="{Binding Icon}"  Width="32" Height="18" />
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>                  
                        <GridViewColumn Width="120" Header="Content Name" DisplayMemberBinding="{Binding ContentName}"/>
                        <GridViewColumn Width="120" Header="Version Name" DisplayMemberBinding="{Binding Name}"/>
                    </GridView>
                </ListView.View>
                <ListView.ItemContainerStyle>       
                    <Style TargetType="{x:TypeListViewItem}">                                        
                        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                        <Setter Property="local:CMListViewLinkedVersionsItemBehavior.IsListViewLinkedVersionsItemBehavior" Value="True"/>
                        <Setter Property="mvvm:EventToCommand.Event" Value="MouseDoubleClick" />
                        <Setter Property="mvvm:EventToCommand.Command" Value="{Binding ElementName=VersionsList, Path=DataContext.OpenLinkedVersionInAnotherWorkspaceCommand}" />
                        <Setter Property="mvvm:EventToCommand.CommandParameter" Value="{Binding OpenLinkedVersionInAnotherWorkspaceCommand}" />

                        <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />                                        
                    </Style>
                </ListView.ItemContainerStyle>                 
            </ListView>

重要的是:

<ListView.InputBindings>
  <KeyBinding Gesture="Delete" Command="{Binding DeleteSelectedVersionsCommand}">      </KeyBinding>
</ListView.InputBindings>

它使我成为一个列表,其中包含我想在删除键上按下命令的项目。 它正在工作,但只有在选择了窗口但是当其中一个元素被选中时它才起作用。

任何想法如何解决这个问题?

0 个答案:

没有答案