WPF输入列表框项目的键绑定

B.B*_*dan 2 c# wpf xaml listbox key-bindings

我有一个WPF应用程序,以编程方式将焦点设置到ListBox项,然后使用UP / DOWN箭头从一个项导航到另一个项。我需要为该适当的Item 实现ENTER Key事件,它应该在ViewModel中触发。ICommand SelectItemCommand

考虑一下ViewModel代码:

public class MobileViewModel
{
    public ObservableCollection<Mobile> MobileCollection { get; set; }

    public MobileViewModel()
    {
        MobileCollection = new ObservableCollection<Mobile>()
        {
            new Mobile() { ID = 1, Name = "iPhone 6S", IsSelected = false },
            new Mobile() { ID = 2, Name = "Galaxy S7", IsSelected = false }                        
        }
    }

    public ICommand SelectItemCommand
    {
        get
        {
            return new DelegatingCommand((obj) =>
            {
                // Enter Key Event Operation
            });
        }
    }

}

public class Mobile
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

XAML代码是

<ListBox ItemsSource="{Binding MobileCollection}" x:Name="KeyListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding SelectItemCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MobileViewModel}}}" CommandParameter="{Binding }">
                <Button.InputBindings>
                    <KeyBinding Key="Enter" Command="{Binding SelectItemCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MobileViewModel}}}" CommandParameter="{Binding }" />
                </Button.InputBindings>
                <Button.Content>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>
                </Button.Content>
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

我的要求是在按下键盘ENTER键时触发ICommand。我在Button内尝试了KeyBinding,但是没有发生。请帮助我。

小智 5

列表框键绑定为

<ListBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding DataContext.SelectItemCommand, ElementName=KeyListBox}" 
        CommandParameter="{Binding SelectedItem, 
            RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}"/>
</ListBox.InputBindings>
Run Code Online (Sandbox Code Playgroud)

您应指定Element Name和并使用进行绑定DataContext。那应该工作

完整的XAML源代码为

<ListBox Name="KeyListBox" ItemsSource="{Binding MobileCollection}" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="300" HorizontalContentAlignment="Stretch">

    <ListBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding DataContext.SelectItemCommand, ElementName=lstBox}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}"/>
    </ListBox.InputBindings>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Button Command="{Binding DataContext.SelectItemCommand, ElementName=lstBox}" CommandParameter="{Binding }" Foreground="Black" Padding="12 10" HorizontalContentAlignment="Left">
                    <Button.Content>
                        <StackPanel>
                            <CheckBox IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Foreground="#404040">
                                <CheckBox.Content>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name, IsAsync=True}" TextWrapping="Wrap" MaxWidth="270" />
                                    </StackPanel>
                                </CheckBox.Content>
                            </CheckBox>
                        </StackPanel>
                    </Button.Content>
                </Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)