2016-09-16 71 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>