WPF placeHolder效果的样式设定


<UserControl x:Class="wpfTestStudio.textPlaceHolderDemo"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:wpfTestStudio"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <VisualBrush Opacity="0.5" TileMode="None" x:Key="visualBrush">
            <VisualBrush.Visual>
                <TextBlock Text="请输入..."/>
            </VisualBrush.Visual>
        </VisualBrush>
        <Style x:Key="placeHolderStyle" TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Text" Value="">
                    <Setter Property="Background" Value="{StaticResource visualBrush}"/>
                </Trigger>
                <Trigger Property="Text" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource visualBrush}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid>
        <TextBox x:Name="textBox" Style="{StaticResource placeHolderStyle}">
        </TextBox>
    </Grid>
</UserControl>


posted @ 2022-10-29 20:29  JohnYang819  阅读(112)  评论(0编辑  收藏  举报