2016-09-16 101 views
1

我有一个WPF应用程序,编程方式我将焦点设置到列表框项目,之后使用向上/向下箭头我正在从一个项目导航到另一个项目。我需要执行ENTER Key事件对于适当的Item,它应该在ViewModel中触发ICommandSelectItemCommandWPF输入KeyBinding列表框项目

考虑视图模型代码:

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> 

我的要求是触发ICommand的,而在键盘上输入键击。我尝试了按钮内的KeyBinding,但没有发生。请帮助我。

回答

1

列表框键绑定是

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

您可以将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>