wpf - 如何处理在 View 模型中的列表框keydown事件以删除listItems?

标签 wpf data-binding mvvm

我有一个列表框,该如何处理页面的相应 View 模型中的keydown事件以删除listItems?
我在 View 中使用了此鳕鱼:

<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>

最佳答案

您需要将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>   

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

关于wpf - 如何处理在 View 模型中的列表框keydown事件以删除listItems?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9783424/

相关文章:

c# - 我们可以通过 MVVM 中的 ICommand 获取按钮单击时的按钮属性(如按钮文本、按钮隐藏状态等)吗

android - 使用自定义 XML 标记和数据绑定(bind)的 Gradle 构建不成功

WPF:输入持续时间(hh:mm:ss)

c# - ItemsControl中使用的DataTemplate中WPF组合框的默认SelectedItem无法正常工作

c# - 以编程方式添加图像并将它们定位在 WPF Canvas 上

wpf - 结合 WPF 的 DataAnnotations 和 IDataErrorInfo

c# - 嵌套网格中的 XAML 数据绑定(bind)

c# - 从 WP8.1 重定向到 WP8 后,ItemsControl 的数据绑定(bind)不起作用

wpf - InvokeCommandAction 未调用

c# - 将资源字典文件动态加载到 wpf 应用程序会出错