Remove default text in multiple XAML Textboxes (on GotFocus), with single code-behind?

时间:2015-07-28 22:16:51

标签: c# wpf xaml

I have the following code behind:

    public void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
    TextBox tb = (TextBox)sender;
    tb.Text = string.Empty;
    tb.GotFocus -= TextBox_GotFocus;
}

My XAML is as follows:

<TextBox x:Name="studyNameBox" Height="20" Margin="30,69,30,0" TextWrapping="Wrap" 
    Text="Study Name" VerticalAlignment="Top" FontSize="13.333" AllowDrop="False" 
    GotFocus="TextBox_GotFocus"/>
<TextBox x:Name="studyFacilNameBox" Height="20" Margin="30,101,30,0" 
    TextWrapping="Wrap" Text="Facilitator Name" VerticalAlignment="Top" FontSize="13.333" 
    GotFocus="TextBox_GotFocus"/>
<TextBox x:Name="studyNotesBox" Margin="30,132.334,30,97" TextWrapping="Wrap" 
    Text="Notes" FontSize="13.333" GotFocus="TextBox_GotFocus"/>

On clicking in the first box (studyNameBox) the default text disappears however it does not work for the other two boxes (studyFacilNameBox/studyNotesBox).

Original code from: Remove text after clicking in the textbox

How should I modify this to get all 3 boxes to clear the default text on getting the focus?

Many thanks.

1 个答案:

答案 0 :(得分:0)

这里有一个仅限XAML的解决方案,你不需要在代码隐藏中做任何花哨的事情来模仿水印。

考虑使用以下样式:

<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"
       x:Key="TextBoxWithWatermarkStyle">
    <Setter Property="Padding" Value="3"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border BorderThickness="1"
                        CornerRadius="1"
                        Background="{TemplateBinding Background}">
                    <Grid>
                        <ScrollViewer Margin="{TemplateBinding Padding}"
                                  x:Name="PART_ContentHost"/>
                        <TextBlock IsHitTestVisible="False" 
                               Text="{TemplateBinding Tag}"
                               VerticalAlignment="Center" 
                               HorizontalAlignment="Left"
                               Opacity="0.25"
                               Foreground="{TemplateBinding Foreground}"
                               Margin="5,0,0,0">
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}" Value="">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用法:

<TextBox Style="{StaticResource TextBoxWithWatermarkStyle}" Tag="My Watermark" ... />