WPF输入列表框项目的键绑定 [英] WPF Input KeyBinding for a ListBox Item

查看:71
本文介绍了WPF输入列表框项目的键绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,以编程方式将焦点设置到ListBox项上,然后使用向上/向下箭头从一个项目导航到另一个项目.我需要为该适当的项实现 ENTER Key Event ,它应该触发ViewModel中的 ICommand SelectItemCommand.

I'm having a WPF Application, programatically I'm setting the focus to the ListBox Item, after that using UP / Down arrow I'm navigating from one Item to another Item. I need to Implement ENTER KeyEvent for that appropriate Item, it should trigger the ICommand SelectItemCommand in the ViewModel.

考虑ViewModel代码:

Consider the ViewModel Code:

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

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>

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

My requirement is to trigger the ICommand while on Keyboard ENTER Key hit. I tried the KeyBinding inside the Button, but its not happening. Kindly assist me.

推荐答案

ListBox键绑定为

The ListBox Key Binding is

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

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

You should specify the Element Name and bind using DataContext. Then It should be work

完整的XAML源代码为

The Complete XAML Source Code is

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

这篇关于WPF输入列表框项目的键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆