KeiStory

WPF TextBox 에 PlaceHolder  설정하기

 

입력 값이 없는 경우 DataTrigger 를 이용해 공백인 경우 문자를 표시하여 PlaceHolder 처럼 표시합니다.

<Window x:Class="PlaceHolder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="86.638" Width="227.155">
    <StackPanel>
        <Grid>
            <TextBox Name="searchTextBox" Margin="10"  Text="" Width="180" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
            <TextBlock Text="Search Name.." IsHitTestVisible="False" VerticalAlignment="Center" Margin="15,10,10,10" HorizontalAlignment="Left">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Visibility" Value="Hidden"/>
                        <Setter Property="Foreground" Value="Gray"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Text, ElementName=searchTextBox}" Value="">
                                <Setter Property="Visibility" Value="Visible"/>
                           </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </Grid>
    </StackPanel>
</Window>
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band