Reputation: 73
I have a window with several textboxes, in which I can't tab trough all of them.
The problem: The first tabbing works, but when pressing tab on the second textbox, the focus is set to the placeholder, and I am somehow editing the placeholder.
Buttons code:
<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}"/>
Placeholder code:
<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>
How can I fix this? Thanks you for your help in advance!
Upvotes: 0
Views: 75
Reputation: 15227
To prevent an UIElement
from receiving the keyboard focus on pressing the Tab key, set its IsTabStop
property to false
.
You can modify your Style
as follows:
<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>
The first setter in the style is to prevent from receiving a tab stop by the actual TextBox
you're applying the Style
to (because you define another one in the ControlTemplate
). The second (placeholder) TextBox
has this property set to false
too.
Upvotes: 1