I'm new to WPF I'm trying to make a custom-style TextBox
to set a hint text. I have a TextBlock
on top of a TextBox
and a trigger that hides the TextBlock
if the value of the TextBox
is not empty.
Here is the style XML:
<Style TargetType="{x:Type TextBox}"
x:Key="SearchBoxTheme">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border CornerRadius="10"
Width="{Binding Width, ElementName=SearchBox}"
Background="#3d5a80">
<Grid>
<Rectangle StrokeThickness="1"/>
<TextBox Margin="1"
FontSize="17"
Foreground="#e0fbfc"
BorderThickness="0"
Background="Transparent"
Padding="5"
x:Name="SearchBox"/>
<TextBlock IsHitTestVisible="False"
Text="Hint Text..."
FontSize="17"
Foreground="#e0fbfc"
Background="Transparent"
Margin="0,0,10,0"
Grid.Column="1">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=SearchBox}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Visibility" Value="Hidden"/>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then, in the code behind, I try to output the text with Debug.WL but it's always empty.
This is the window XML:
<Grid>
<TextBox x:Name="txtbox" HorizontalAlignment="Right" Height="50" Width="200" Margin="0,0,20,30" Style="{StaticResource SearchBoxTheme}" TabIndex="3"></TextBox>
<Button Height="50" Width="50" Click="Button_Click" />
</Grid>
Code behind:
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtbox.Text == "") Debug.WriteLine("NOTHING");
else Debug.WriteLine(txtbox.Text);
}