我有一个列表框,该如何处理页面的相应 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/