【问题标题】:Tabbing trough textboxes not working properly通过文本框切换无法正常工作
【发布时间】:2017-02-09 09:37:18
【问题描述】:

我有一个带有多个文本框的窗口,我无法在其中使用 Tab 键查看所有文本框。

问题:第一个制表符有效,但是在第二个文本框上按制表符时,焦点设置为占位符,我以某种方式编辑占位符。

按钮代码:

 <TextBox x:Name="nazivPodjetja_tb" HorizontalAlignment="Left" Height="23" Margin="10,70,0,0" TextWrapping="Wrap" Tag="Naziv podjetja" VerticalAlignment="Top" Width="219" ToolTip="Naziv podjetja" Style="{StaticResource placeHolderNoline}"/>
    <TextBox x:Name="ime_tb"  HorizontalAlignment="Left" Height="23" Margin="10,100,0,0" TextWrapping="Wrap"  Tag="Ime" VerticalAlignment="Top" Width="219"  SelectionOpacity="-14" ToolTip="Ime" Style="{StaticResource placeHolderNoline}"/>
    <TextBox x:Name="priimek_tb" HorizontalAlignment="Right" Height="23" Margin="0,100,10,0" TextWrapping="Wrap" Tag="Priimek" VerticalAlignment="Top" Width="219"  ToolTip="Priimek" Style="{StaticResource placeHolderNoline}"/>
    <TextBox x:Name="gsm_tb" HorizontalAlignment="Left" Height="23" Margin="10,128,0,0" TextWrapping="Wrap" Tag="Mobilna številka" VerticalAlignment="Top" Width="219" ToolTip="Mobilna številka" Style="{StaticResource placeHolderNoline}"/>
    <TextBox x:Name="eNaslov_tb" HorizontalAlignment="Right" Height="23" Margin="0,128,10,0" TextWrapping="Wrap" Tag="E-naslov" VerticalAlignment="Top" Width="219"  ToolTip="E-naslov" Style="{StaticResource placeHolderNoline}"/>
    <TextBox x:Name="stacionarni_tb" HorizontalAlignment="Left" Height="23" Margin="10,156,0,0" TextWrapping="Wrap" Tag="Stacionarna številka" VerticalAlignment="Top" Width="219" ToolTip="Stacionarna številka" Style="{StaticResource placeHolderNoline}"/>
    <TextBox x:Name="naslov_tb" HorizontalAlignment="Right" Height="23" Margin="0,156,10,0" TextWrapping="Wrap" Tag="Naslov" VerticalAlignment="Top" Width="219"  ToolTip="Naslov" Style="{StaticResource placeHolderNoline}"/>
    <ComboBox x:Name="skupina_cbx" Grid.Column="1" HorizontalAlignment="Left" Margin="10,185,0,0" VerticalAlignment="Top" Width="219" Style="{DynamicResource ComboBox}"/>
    <TextBox x:Name="posta_tb" HorizontalAlignment="Right" Height="23" Margin="0,184,10,0" TextWrapping="Wrap" Tag="Pošta" VerticalAlignment="Top" Width="219"  AutomationProperties.Name="Pošta" ToolTip="Pošta" Style="{StaticResource placeHolderNoline}"/>

占位符代码:

<Style x:Key="placeHolderNoline" TargetType="{x:Type TextBox}" BasedOn="{StaticResource tb}">

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <TextBox Text="{Binding Path=Text,
                                            RelativeSource={RelativeSource TemplatedParent}, 
                                            Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged}"
                             x:Name="textSource" 
                             Background="Transparent" 
                             Panel.ZIndex="2" 
                                 BorderThickness="0,0,0,0"/>
                            <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1" BorderThickness="0">
                                <TextBox.Style>
                                    <Style TargetType="{x:Type TextBox}">
                                        <Setter Property="Foreground" Value="Transparent"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                                <Setter Property="Foreground" Value="LightGray"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我该如何解决这个问题?提前感谢您的帮助!

【问题讨论】:

    标签: wpf xaml textbox


    【解决方案1】:

    要防止UIElement 在按下Tab 键时接收键盘焦点,请将其IsTabStop 属性设置为false

    你可以修改你的Style如下:

    <Style x:Key="placeHolderNoline" TargetType="{x:Type TextBox}">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    ...
                   <TextBox/> 
                   <TextBox IsTabStop="False"/>
                   ...
               </ControlTemplate>
           </Setter.Value>
       </Setter>
    </Style>
    

    样式中的第一个 setter 是为了防止收到 actual TextBox 的制表位,您正在将 Style 应用到(因为您在 ControlTemplate 中定义了另一个)。第二个(占位符)TextBox 也将此属性设置为 false

    【讨论】:

    • 我不确定我编辑的模板是否正确,但它不起作用。现在,在选项卡之后,它首先开始编辑占位符,然后在第二个选项卡上编辑内容。请检查我编辑的代码的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多