2011-10-31 22 views
5

Tôi có một User Control như thế này:Đặt hàng tab trong wpf khi sử dụng từ UserControls?

<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> 

Và tôi sử dụng kiểm soát này ở cửa sổ và thiết lập lệnh tab ... nhưng khi cửa sổ của tôi được tải, thứ tự tab không hoạt động một cách chính xác !!! mã cửa sổ của tôi:

<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> 

Trả lời

7

Theo mặc định, WPF đọc tất cả các điều khiển, bên trong và bên ngoài UserControl của bạn, ở cấp tab cùng. Vì các điều khiển bên trong UserControl của bạn không có một TabIndex được chỉ định, chúng sẽ được tab để kéo dài sau chu kỳ tab đầu tiên.

Cách giải quyết Tôi thường sử dụng là để thiết lập IsTabStop="False" trên tôi UserControl (để ngăn chặn tabbing vào UserControl chính nó), và sau đó bên trong UserControl sử dụng một TemplateBinding để ràng buộc sang Kiểm soát nội TabIndex đến của UserControl 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

bể Bạn Rachel..its hoạt động hoàn hảo –

+0

hoạt động tốt đối với tôi! – psulek

+0

Hoặc bạn có thể sử dụng 'KeyboardNavigation.TabNavigation =" Local "' trên '' khi usercontrol tùy chỉnh của bạn có nhiều điều khiển hơn có thứ tự tab cụ thể của chúng chỉ hợp lệ với usercontrol – psulek

Các vấn đề liên quan