【问题标题】:Setting tab order in wpf when use from UserControls?从 UserControls 使用时在 wpf 中设置制表符顺序?
【发布时间】:2011-12-18 18:59:30
【问题描述】:

我有一个这样的用户控件:

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

我在窗口中使用此控件并设置选项卡顺序...但是当我的窗口加载时,选项卡顺序无法正常工作!!! 我的窗口代码:

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

【问题讨论】:

    标签: c# wpf user-controls tab-ordering


    【解决方案1】:

    默认情况下,WPF 在同一选项卡级别读取用户控件内部和外部的所有控件。由于您的 UserControl 中的控件没有指定 TabIndex,因此它们会在第一个选项卡循环后被标记为持续。

    我通常使用的解决方法是在我的UserControl 上设置IsTabStop="False"(以防止在UserControl 本身上使用标签),然后在UserControl 内部使用TemplateBinding 绑定到内部控件@987654325 @到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"/>
    

    【讨论】:

    • 或者你可以在&lt;my:usercontrol&gt; 上使用KeyboardNavigation.TabNavigation="Local",当你的自定义用户控件有更多的控件时,这些控件有自己的特定标签顺序,仅对用户控件有效
    • @psulek:我建议您将此作为答案,它对我的​​情况有所帮助,也许其他人会发现它很有用(用户控件中的几个用户控件,再加上一些常规小部件)。跨度>
    • @psulek,几周来我一直在互联网上寻找解决方案....答案就在评论上。你救了我……非常感谢……
    猜你喜欢
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    相关资源
    最近更新 更多