2010-07-01 34 views
2

我有一个列表框,其项目模板有一个复选框。 现在,当我单击列表框中的复选框时,它将设置复选框的选中状态。如果我使用键盘“空格”键,我无法更改复选框状态。键盘焦点列出WPF中的框项目

注意:通过单击将焦点设置到复选框,键盘快捷键正在工作。

回答

2

如果你不想列表框,提供选择的一切,你可以使用一个普通的ItemsControl,而不是一个列表框:

<ItemsControl ItemsSource="{Binding}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Content="{Binding}"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

然后,你将只是一个复选框序列不与ListBoxItem的包裹他们控件将采取键盘焦点。另一方面,如果你想要ListBox显示选择,那么你可能想要一个多选列表框,其中CheckBox的状态被绑定到ListBoxItem的选定状态。由于单击ListBoxItem将检查CheckBox,因此可以防止CheckBox被完全集中:

<ListBox ItemsSource="{Binding}" SelectionMode="Multiple"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox 
       Content="{Binding}" 
       Focusable="False" 
       IsChecked="{Binding IsSelected, RelativeSource= 
        {RelativeSource AncestorType={x:Type ListBoxItem}}}" /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
相关问题