https://code.4noobz.net/wpf-add-a-watermark-to-a-native-wpf-textbox/

 

WPF - Add a Watermark to a native WPF TextBox - Code4Noobz

This is how to add a Watermark to a native WPF TextBox without using a control from another toolkit.  The concept is to create a Style which will add a new property (called Tag) for your TextBoxes. The content of this Tag will be the watermark. And it wil

code.4noobz.net

<Window.Resources>
        <Style x:Key="MyWaterMarkStyle" TargetType="{x:Type TextBox}">   
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <Border Background="White" BorderBrush="#FF7D8683" BorderThickness="1"/>
                            <ScrollViewer x:Name="PART_ContentHost" Margin="5,0,0,0" VerticalAlignment="Center" />
                            <Label Margin="5,0,0,0" x:Name="WaterMarkLabel" Content="{TemplateBinding Tag}" VerticalAlignment="Center"
                               Visibility="Collapsed" Foreground="Gray" FontFamily="Arial"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Text" Value=""/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="DimGray"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

<Grid >
   <TextBox Style="{StaticResource MyWaterMarkStyle}" Height="25" Tag="Please write something here!"/>

'C#(.Net)' 카테고리의 다른 글

wpf View, Control 분리하여 Main xaml에 두 컨트롤 xaml 로드  (0) 2023.06.04
wpf 참조  (0) 2021.08.01
WPF) 아날로그시계( c# 200제)  (0) 2021.07.21
C# 기본 get, set (c++ getter, setter)  (0) 2021.07.13
wpf 바인딩 연습1  (0) 2021.07.12

+ Recent posts