2009-12-11 62 views
4

我有一個簡單的窗口,裏面嵌入了一個簡單的複合控件。複合控件中的WPF TabIndex

(主窗口)

<Window x:Class="TabOrder.Window1" 
xmlns:local="clr-namespace:TabOrder" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 
<Grid> 
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">First</Label> 
    <TextBox TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/> 

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Second</Label> 
    <TextBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/> 

    <local:MyControl Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" TabIndex="2"/> 
</Grid> 

(複合控件)

<UserControl x:Class="TabOrder.MyControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Grid> 
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">Third</Label> 
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/> 

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Fourth</Label> 
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/> 
</Grid> 

正如預期的那樣我的形式,我得到4個文本框...

  • 首先

但是,當「第一」具有焦點和我打標籤的焦點切換到「第三」。 WPF似乎將選項卡列表看作一個單一列表而不是樹,其中MyControl是TabIndex 3,文本框「Third」是其中的第一個選項卡式控件。

這是一個WPF中的錯誤還是有另一種方法呢?複合控件被用在很多窗口中,它甚至可以在單個窗口上多次使用。

回答

11

我知道這反應相當晚了......但你嘗試過:

<UserControl ... KeyboardNavigation.TabNavigation="Local"> 

這樣做將確保一旦你的用戶控件已收到焦點時,您將您的用戶控件內只有通過導航接受tab(而不是杞人憂天關於整個應用程序中衝突的TabIndex值)。在遍歷UserControl的TabStops之後,TabNavigation將恢復到TabStop之外。

http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigationmode.aspx

+1

這對我來說並不遲到。謝謝。 – zgnilec 2014-08-17 00:00:27