标签槽文本框无法正常工作

时间:2017-02-09 09:37:19

标签: wpf xaml textbox

我有一个带有几个文本框的窗口,我无法在其中删除所有文本框。

问题:第一个标签工作,但是当按第二个文本框上的标签时,焦点设置为占位符,我以某种方式编辑占位符。

按钮代码:

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

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

1 个答案:

答案 0 :(得分:1)

要防止UIElement在按下标签键时获得键盘焦点,请将其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>

样式中的第一个设置者是阻止您通过实际 TextBox接收制表位,因为您正在应用Style(因为您定义了另一个ControlTemplate)。第二个(占位符)TextBox也将此属性设置为false