2011-10-31 59 views
5

我有一個用戶控制是這樣的:從UserControls使用時,在wpf中設置tab鍵順序?

<UserControl x:Class="MySample.customtextbox" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="20" d:DesignWidth="300"> 
<Grid> 
    <TextBox x:Name="Ytextbox" Background="Yellow"/> 
</Grid> 

而且我用的這個窗口控制和設置選項卡訂單...但我的加載窗口時,Tab鍵順序不能正常工作! 我的窗口代碼:

<Window xmlns:my="clr-namespace:MySample" x:Class="MySample.window" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="window" Height="300" Width="600"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 

    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition></ColumnDefinition> 
     <ColumnDefinition></ColumnDefinition> 
     <ColumnDefinition></ColumnDefinition> 
     <ColumnDefinition></ColumnDefinition> 
    </Grid.ColumnDefinitions> 

    <my:customtextbox Grid.Column="1" KeyboardNavigation.TabIndex="0" InfoText="{Binding msg}" Height="20"/> 
    <TextBox Grid.Column="3" KeyboardNavigation.TabIndex="1" Text="{Binding msg}" Height="20" Background="Gold"></TextBox> 
    <my:customtextbox Grid.Row="1" Grid.Column="1" KeyboardNavigation.TabIndex="2" InfoText="{Binding msg}" Height="20"/> 
    <TextBox Grid.Column="3" Grid.Row="1" Text="{Binding msg}" Height="20" KeyboardNavigation.TabIndex="3" Background="Gold"></TextBox> 

</Grid> 

回答

7

默認情況下,WPF讀取所有的控制,內部和外部的用戶控件,在同一標籤的水平。由於UserControl中的控件沒有指定TabIndex,因此它們會在第一個製表符週期後得到選項卡。

的解決方法,我通常使用的是設置在IsTabStop="False"UserControl(防止跳格到用戶控件本身),然後UserControl內使用TemplateBindingTabIndex結合內部控制的用戶控件的TabIndex

<TextBox x:Name="Ytextbox" Background="Yellow" 
     TabIndex="{Binding Path=TabIndex, 
     RelativeSource={RelativeSource AncestorType={x:Type local:customtextbox}}}"/> 

<my:customtextbox IsTabStop="False" KeyboardNavigation.TabIndex="0" 
        Grid.Column="1" InfoText="{Binding msg}" Height="20"/> 
+0

坦克您Rachel..its工作完全 –

+0

很適合我! – psulek

+0

或者當您的自定義usercontrol具有更多控件時,您可以在''上使用'KeyboardNavigation.TabNavigation =「Local」'''''''''' – psulek

相關問題