<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>
标签:样式,WPF,placeHolder,设定,效果
From: https://www.cnblogs.com/johnyang/p/16839785.html