2011-04-05 36 views
4

我有一個WPF中的視圖,我一直在爭取讓Tab鍵順序正確。我有三個文本框(讓我們稱它們爲Text1,Text2和Text3)以及兩個自定義控件,每個自定義控件都有幾個其他文本框和各種控件(讓它們稱爲Custom1和Custom2)。WPF標籤順序工作出錯

佈局是這樣的標籤流應該去Text1,Text2,Custom1,Custom2,Text3。我在每個控件上設置了TabIndex屬性以匹配此排序,並驗證它們全部都設置爲IsTabStop。

問題是,實際的選項卡流程是Text1,Text2,Text3,然後是Custom1,Custom2,我找不到原因。當它進入自定義控件時,它會像我期望的那樣正確地逐步控制它們中的每個控件。在第一個自定義控件出現之前,我無法弄清楚爲什麼會出現在第三個文本框中。

我已經嘗試了所有我能想到的方法,包括確保所有xaml元素都按標籤順序排列,但似乎沒有任何幫助。

我懷疑它將重點放在任何自定義控件之前將其所有的基本控件都考慮在內,但我沒有想法。任何幫助都會很棒。

編輯: 這是我的XAML:

<Grid> 
    <GroupBox x:Name="_groupBox" BorderBrush="Transparent" BorderThickness="0"> 
     <Grid x:Name="_card"> 
      <Label Content="A:" Height="28" HorizontalAlignment="Left" Margin="5,3,0,0" Name="_labelA" VerticalAlignment="Top" /> 
      <Label Content="B:" Height="28" HorizontalAlignment="Left" Margin="5,25,0,0" Name="_labelB" VerticalAlignment="Top" /> 
      <TextBox Name="_a" Height="20" HorizontalAlignment="Left" Text="{Binding AText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding AEnabled}" Margin="94,5,0,0" VerticalAlignment="Top" LostFocus="InputNameLeave" Width="221" TabIndex="0" /> 
      <TextBox Name="_b" Height="20" HorizontalAlignment="Left" Margin="94,26,0,0" Text="{Binding BText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="102" TabIndex="1" /> 
      <my:CustomControlA HorizontalAlignment="Left" Margin="-6,55,0,0" x:Name="_custom1" VerticalAlignment="Top" TabIndex="2" IsTabStop="True" /> 
      <my:CustomControlB HorizontalAlignment="Left" Margin="334,0,0,0" x:Name="_custom2" VerticalAlignment="Top" Width="320" TabIndex="3" IsTabStop="True" /> 
      <Label Content="C:" Height="28" HorizontalAlignment="Left" Margin="342,59,0,0" Name="_labelC" VerticalAlignment="Top" /> 
      <TextBox Name="_c" Height="20" HorizontalAlignment="Left" Margin="417,60,0,0" Text="{Binding CText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding CEnabled}" VerticalAlignment="Top" Width="154" TabIndex="4" /> 
     </Grid> 
    </GroupBox> 
</Grid> 
+4

你可以發表你的XAML? – Rachel 2011-04-05 18:06:34

回答

10

我沒有你的自定義控制手,所以我創建了一個包含只是一個TextBox。在將它連接到您的XAML後,我發現Tab鍵順序如下:

TextBox1,TextBox2,CustomControl1,CustomControl2,TextBox3,CustomControl1內的TextBox,CustomControl2內的TextBox。

請注意,TextBox2標籤將焦點移動到後,自定義控件自己控制,而不是它們碰巧具有的任何子控件。我的自定義控件中的TextBox沒有TabIndex,所以它的TabIndex被認爲是默認值Int32.MaxValue(請參閱MSDN documentation for the TabIndex property)。因此,它們以Tab鍵順序排在最後。

我發現,如果我將自定義控件標記爲IsTabStop="False"(我不明白爲什麼我希望自己專注於自定義控件),並在自定義中設置TextBox的Tab-index通過將TabIndex="{TemplateBinding TabIndex}"屬性添加到自定義控件中的文本框來控制<my:CustomControl />元素中給出的控件。一旦我完成了,Tab鍵順序如我所料,

TextBox1,TextBox2,CustomControl1中的TextBox,CustomControl2中的TextBox,TextBox3。

當然,我的自定義控件只包含一個單獨的TextBox,因此設置一個tab-index固定的東西給我。我沒有你的代碼,所以我不能肯定地說,但用同樣的方法將你自定義控件中的所有子控件的tab索引設置爲<my:CustomControl />元素中的tab-index就足夠了。

編輯:如果你不能使用TemplateBinding,而是有相應的.xaml.cs文件的.xaml文件,那麼你有什麼叫做用戶控制,而不是一個自定義的控制。在這種情況下, 你可以嘗試使用類似下面的設置XAML內的標籤索引CustomControlA:

TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type my:CustomControlA}}}" 
+0

我的自定義控件不喜歡TemplateBinding,因爲我沒有使用模板。還有其他的方式來繼承它,還是我需要模板化呢?這是問題,但如果我將相應的父窗體的正確Tab鍵順序編碼到子控件控件中,它將起作用。我現在只需要使其更加普遍化。 – captncraig 2011-04-06 17:12:39

+0

我剛剛遇到類似的問題,使用自定義控件的TabIndex =「{TemplateBinding TabIndex}」丟失。謝謝一堆! – louisgab 2011-10-25 16:53:04

1

請發表您的XAML代碼。在此期間,一些事情要考慮的問題包括:

  • 檢查KeyboardNavigation.TabIndex屬性值一次
  • 你在運行時動態添加自定義選項卡?
  • 您是否在運行時以編程方式更改自定義控件或與其進行交互?
  • 確保標籤以正確的順序列出,在XAML標記