WPF中设置Tab键顺序,当用户控件从使用? [英] Setting tab order in wpf when use from UserControls?
问题描述
我有一个用户控制是这样的:
I have a User Control like this:
<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键顺序不能正常工作!
我的窗口代码:
And I use this control in a window and set tab orders...but when my window is loaded, the tab order is not working correctly!!! my window code:
<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>
推荐答案
默认情况下,WPF读取所有的控制,内部和外用户控件在同一标签的水平。由于您的用户控件内部的控件没有规范就在tabIndex,他们得到标签到最后第一个标签循环后。
By default, WPF reads all the controls, inside and outside your UserControl, at the same tab level. Since the controls inside your UserControl do not have a TabIndex specified, they get tabbed to last after the first tab cycle.
我通常使用的解决方法是将 IsTabStop =FALSE
我的用户控件
(防止tab键切换到用户控件本身),然后在<$ C $内C>用户控件使用 TemplateBinding
绑定到内部控制的TabIndex
到用户控件的的TabIndex
The workaround I usually use is to set the IsTabStop="False"
on my UserControl
(to prevent tabbing onto the UserControl itself), and then inside the UserControl
use a TemplateBinding
to bind to the inner Controls TabIndex
to the UserControl's 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"/>
这篇关于WPF中设置Tab键顺序,当用户控件从使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!