如何在viewmodel中处理listbox keydown事件以删除listItems?

Tul*_*lsi 0 data-binding wpf mvvm

我有一个列表框如何在页面的相应视图模型中处理keydown事件以删除listItems?我在视野中使用这种鳕鱼:

<ListBox Margin="2,25,2,2" Grid.Row="3" ItemsSource="{Binding Path=CardViewModelSearchResults}" SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True">
            <ItemsControl.ItemTemplate>
                <DataTemplate >
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Row="0" Grid.Column="0" ff:TextBoxBehaviour.TextChangedCommand="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}, Path=DataContext.TextChanged}" Text="{Binding Path=CategoryName}" FontSize="14" FontWeight="Normal" BorderThickness="0" AllowDrop="False" />
                        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=CategoryID}" FontSize="14" FontWeight="Normal" Visibility="Hidden" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>
Run Code Online (Sandbox Code Playgroud)

Ank*_*esh 6

您需要将KeyBinding添加到您的应用程序或专门添加到ListBox.

列表框

<ListBox Margin="2,25,2,2" Grid.Row="3"  ItemsSource="{BindingPath=CardViewModelSearchResults}" 
                 SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}" 
                IsSynchronizedWithCurrentItem="True">
            <ListBox.InputBindings>
                <KeyBinding Key="Delete" Command="{Binding Path=MyDeleteCommand}" />
            </ListBox.InputBindings>
    </ListBox>   
Run Code Online (Sandbox Code Playgroud)

如果在.net4.0中使用.net 3.5,则需要使用Command引用