2010-08-01 26 views
0

我有一個WPF應用程序與ListBox顯示項目列表。 每個項目都有IsChecked屬性。專注於按鈕ListBoxItem內沒有代碼

我改變列表框的ItemContainerStyle的風格follos:

<Style x:Key="OrgListItemStyle" TargetType="ListBoxItem"> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <ToggleButton IsChecked="{Binding IsChecked}"> 
        <ContentPresenter /> 
       </ToggleButton> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

的問題是關注的焦點,用鍵盤導航時,是在ListBoxItem的,而不是切換按鈕本身,這使得與其合作並不直觀。

如何更改焦點,以便它在按鈕上而不是ListBoxItem - 最好不要與代碼。

謝謝 伊

回答

1

您可以設置Focusable爲false,在樣式的ListBoxItems:

<Setter Property="Focusable" Value="False"/> 

注意,列表框有一些神奇的是要記住,必須贏得」鍵盤焦點的元素如果你不允許ListBoxItem被選中,你就可以工作。

如果你不想使用ListBox的功能,那麼你最好使用普通的ItemsControl而不是ListBox。您可能需要使用一個DataTemplate,而不是一個控件模板,因爲沒有容器控件:

<ItemsControl ItemsSource="{Binding Items}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ToggleButton IsChecked="{Binding IsChecked}" Content="{Binding}"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

謝謝,我真的讓自己緊張起來 - ItemsControl的是爲這個問題的解決方案。 – 2010-08-03 10:08:00