繁体   English   中英

列表框项的 WPF 输入键绑定

[英]WPF Input KeyBinding for a ListBox Item

我有一个 WPF 应用程序,以编程方式我将焦点设置到 ListBox 项目,然后使用向上/向下箭头我从一个项目导航到另一个项目。 我需要为那个合适的项目实现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; }
}

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,但它没有发生。 请帮助我。

ListBox 键绑定是

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

您应该指定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>

您可以将 InputBinding 放在 ListBox 本身上,将所选项目作为命令参数传递。

<ListBox ItemsSource="{Binding MobileCollection}" x:Name="KeyListBox">
    <ListBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding SelectItemCommand}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}"/>
    </ListBox.InputBindings>

</ListBox>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM