在样式中使用附加属性
本文关键字:属性 样式 | 更新日期: 2023-09-27 18:02:03
我试图使用附加属性的文本框样式,以启用使用水印文本。然而,我似乎不能让它工作。工程建立良好,但水印不显示。有人有什么想法吗?谢谢。
WatermarkProperty.cs
public class WatermarkProperty
{
public static string GetWatermark(DependencyObject obj)
{
return (string)obj.GetValue(WatermarkProp);
}
public static void SetWatermark(DependencyObject obj, string value)
{
obj.SetValue(WatermarkProp, value);
}
public static readonly DependencyProperty WatermarkProp =
DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(WatermarkProperty), new UIPropertyMetadata(string.Empty));
}
MainWindow.xaml
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Background">
<Setter.Value>
<VisualBrush>
<VisualBrush.Visual>
<Label Content="{Binding Path=(local:WatermarkProperty.Watermark), RelativeSource={RelativeSource Self}}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<TextBox local:WatermarkProperty.Watermark="Testing" Width="200"/>
</Grid>
</Window>
要为watermark
的textbox
创建template
,您需要类似于下面的style
的样式(此样式已经过测试并且有效):
<Style TargetType="{x:Type TextBox}" x:Key="myTextBoxStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Grid x:Name="PART_InnerGrid"
Margin="2">
<ScrollViewer x:Name="PART_ContentHost"
Grid.Column="0"
BorderThickness="0"
IsTabStop="False"
VerticalAlignment="Center"
Background="{x:Null}" />
<TextBlock x:Name="Message"
Grid.Column="0"
Text="{TemplateBinding local:TextBoxHelper.Watermark}"
Padding="{TemplateBinding Padding}"
Visibility="Collapsed"
Foreground="{TemplateBinding Foreground}"
IsHitTestVisible="False"
Opacity="0.6"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="Center"
Margin="6,2,6,2" />
<ContentControl/>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}"
Value="">
<Setter TargetName="Message"
Property="Visibility"
Value="Visible" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
要使style
对textbox
产生影响,需要手动设置:
<TextBox local:TextBoxHelper.Watermark="Testing" Width="200" Height="40" Style="{StaticResource myTextBoxStyle}"/>
我在它的WatermarkProperty
类中实现了一些修改。这些更改都不会影响类的功能,但是要遵循在创建"帮助"control
结构的类时使用的命名标准。下面的新类现在称为TextBoxHelper
:
public class TextBoxHelper
{
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), new UIPropertyMetadata(string.Empty));
}
更改WatermarkProperty
到TextBoxHelper
的名称,因为该类帮助textbox
控件。并且还将attached property
的名称修改为WatermarkProperty
,因此遵守所有标准的命名法。
不能使用这样的附加属性。你只需要直接使用它们作为普通的XAML属性:
<Label local:WatermarkProperty.Watermark="{Binding ...}" />
如果你想成为一个好公民,你的WatermarkProperty应该是静态的