0

Trying to get to the TextBlock below in my code behind so i can set its text programmatically. However the x:name "HintText" does not show up. Is it because its part of the styling of the TextBox?

        <TextBox x:Name="SearchTextBox"
             Grid.Column="2"
             TextAlignment="Left"
             VerticalContentAlignment="Center"
             BorderThickness="0"
             FontFamily="{DynamicResource RobotoFont}"
             Foreground="{DynamicResource EclipseGray}"
             FontSize="18"
             TextChanged="SearchTextBox_OnTextChanged"
             GotFocus="SearchTextBox_OnGotFocus"
             LostFocus="SearchTextBox_OnLostFocus">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Background" Value="White"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text.IsEmpty}" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                    <VisualBrush.Visual>
                                        <TextBlock x:Name="HintText"
                                                   VerticalAlignment="Center"
                                                   FontSize="16"
                                                   Foreground="LightGray"
                                                   FontFamily="{DynamicResource RobotoFont}">
                                            Search for a word or phrase...
                                        </TextBlock>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
bryanforst
  • 67
  • 1
  • 7

2 Answers2

0

I had a similar issue. Try saving the files, that should allow the Intelisense to find it. It solved my problem.

joannarch
  • 23
  • 4
0

found a link to an answer thanks to a colleague. How can I add a hint text to WPF textbox?

The answer by Martin Schmidt is the one I used. Noticed that the previous implementation was probably taken from this link as well. But it hid access to the hint text making it impossible to change in the code. This solution is much simpler.

My code now looks like this:

<Grid Background="White" HorizontalAlignment="left" VerticalAlignment="Center"  >
        <TextBox x:Name="SearchTextBox"
             TextAlignment="Left"
             VerticalContentAlignment="Center"
             BorderThickness="0"
             FontFamily="{DynamicResource RobotoFont}"
             Foreground="{DynamicResource EclipseGray}"
             FontSize="18"
             TextChanged="SearchTextBox_OnTextChanged"
             GotFocus="SearchTextBox_OnGotFocus"
             LostFocus="SearchTextBox_OnLostFocus"/>
        <TextBlock x:Name="HintText"
            Margin="10,2" 
            MinWidth="50" 
            FontSize="16"
            IsHitTestVisible="false"
            FontFamily="{DynamicResource RobotoFont}"
            Foreground="LightGray"
            Visibility="{Binding ElementName=SearchTextBox, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
    </Grid>
</Gr
bryanforst
  • 67
  • 1
  • 7