2013-01-11 59 views
3

我看到奇怪的行爲與我的CheckBox及其焦點/標籤順序。複選框失去焦點FocusManager.IsFocusScope =「True」

首先是一些「工作」代碼:

<Grid>  
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Button Grid.Row="0" Width="100" Height="25"/> 
    <TabControl Grid.Row="1" > 
     <!--TabItem Header="tabItem1" Name="tabItem1"--> 
     <TabItem Header="tabItem1" Name="tabItem1" FocusManager.IsFocusScope="True"> 
      <ScrollViewer> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 

        <TextBox Grid.Row="0" /> 
        <TextBox Grid.Row="1"/> 
        <CheckBox Grid.Row="2" Content="Test" /> 
        <TextBox Grid.Row="3"/> 
       </Grid> 
      </ScrollViewer> 
     </TabItem> 
    </TabControl> 
</Grid> 

如果你試試這個,Tab鍵順序工作正常 - 只要你不選中複選框。如果我檢查複選框,它會失去焦點,下一個標籤頁會將焦點設置到按鈕上。

如果我刪除FocusManager.IsFocusScope="True"所有工作正常。

我的問題是此行爲通緝或錯誤?

回答

4

此行爲是某種預期。爲了解決這個問題,你可以在窗口中添加GotFocus的處理程序。

假設您的複選框被命名爲chkBox,這樣的事情:

protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) 
{ 
    base.OnGotKeyboardFocus(e); 

    if (e.Source == chkBox) 
     FocusManager.SetFocusedElement(this, chkBox); 

} 

這個問題和一些類似的有更詳細地討論this msdn thread

+0

這會工作。但我仍然想知道爲什麼這只是複選框沒有其他輸入控件發生。 – blindmeis

+0

這也會發生在RadioButton等其他控件上,在鏈接的msdn線程中查看Weifen Luo的回答,它對發生這種情況的原因有很好的解釋。 –