2010-08-01 3 views
0

항목 목록을 표시하는 ListBox가있는 WPF 응용 프로그램이 있습니다. 각 항목에는 IsChecked 속성이 있습니다.코드없이 ListBoxItem 내부의 단추에 초점 맞추기

나는 follos으로 목록 상자의 ItemContainerStyle의 스타일을 변경할 수 있습니다

<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의 아닌 ToggleButton을 자체에 있다는 것이다 직관적으로 작업 할 수 없습니다.

포커스가 어떻게 변경되어 ListBoxItem이 아니라 바로 바뀔 수 있습니다. 코드가 아닌 것이 바람직합니다.

는 이도에게 감사

당신은 당신의 스타일에 ListBoxItems false로 Focusable을 설정할 수

답변

1

: 목록 상자가 '원 키보드 포커스를 가지고있는 요소를 기억해야 할 몇 가지 마법을 가지고

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

주 ListBoxItem을 선택하지 않으면 작동하지 않습니다.

ListBox의 기능을 전혀 사용하지 않으려면 ListBox가 아닌 일반 ItemsControl을 사용하는 것이 좋습니다. 컨테이너 컨트롤이 없기 때문에 ControlTemplate이 아닌 DataTemplate을 사용해야합니다.

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

감사합니다. ItemsControl은이 문제에 대한 올바른 해결책이었습니다. –

관련 문제