网上找到的多是使用VisualBrush,代码如下。单独使用方便,但加入样式模板文字内容无法绑定附加属性(或依赖属性)。
<TextBox HorizontalAlignment="Center" VerticalAlignment="Center" Width="100">
<TextBox.Resources>
<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="提示文字"/>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
本次找到的解决方法 附加属性+转换器+样式
可以多次使用,更改时只需要修改Watermark="背景文字"内容即可。
- 附加属性
public class TextBoxHelper
{
#region Watermark
public static string GetWatermark(DependencyObject obj)
{
return (string)obj.GetValue(WatermarkProperty);
}
public static void SetWatermark(DependencyObject obj, string value)
{
obj.SetValue(WatermarkProperty, value);
}
public static readonly DependencyProperty WatermarkProperty =
DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextBoxHelper));
#endregion
}
- textBox式样
<Style x:Key="HelperText" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border x:Name="BackBorder" SnapsToDevicePixels="true" Background="{TemplateBinding Background}"
CornerRadius="4"
BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<TextBlock x:Name="tbWatermark"
Padding="2,0"
Margin="{TemplateBinding Padding}"
Foreground="{TemplateBinding Foreground}"
FontSize="{TemplateBinding FontSize}"
TextWrapping="Wrap"
Focusable="False"
IsHitTestVisible="False"
Visibility="Collapsed"
Text="{Binding Path=(local:TextBoxHelper.Watermark),RelativeSource={RelativeSource AncestorType=TextBox}, Mode=OneWay}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Opacity="0.3" />
<Border x:Name="underline" IsHitTestVisible="False" Background="Transparent" BorderBrush="#737B8C" BorderThickness="0 0 0 1" CornerRadius="4"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Text,RelativeSource={RelativeSource Self},Converter={StaticResource IsNullOrEmptyConverter}}"
Value="True">
<Setter Property="Visibility"
TargetName="tbWatermark"
Value="Visible" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
- 非空验证转换器
#region String IsNullOrEmpty
internal class IsNullOrEmptyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty((string)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
xaml中使用
<TextBox Grid.Row="4" Style="{StaticResource HelperText}" Grid.Column="1" Height="24" Width="120" Res:TextBoxHelper.Watermark="背景文字"/>