0

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);
    }
Connor Low
  • 5,900
  • 3
  • 31
  • 52
  • 1
    Have you seen [this](https://stackoverflow.com/q/7425618/1136211)? Besides that, you should not have a TextBox in the ControlTemplate of a TextBox. There should instead be `` – Clemens Sep 15 '21 at 10:53
  • Can this perhaps help? https://stackoverflow.com/questions/11043278/cant-get-text-value-from-control-template or this? https://stackoverflow.com/questions/69190145/why-cant-i-write-and-display-text-in-my-textbox-what-is-made-with-an-style-and-a – tombobadil Sep 15 '21 at 11:02
  • This worked perfect especially for the tag idea so I can reuse it https://stackoverflow.com/a/21439310/13211556 – Khaled Ayman Sep 16 '21 at 01:55

0 Answers0