在样式中使用附加属性

时间:2015-07-10 18:34:48

标签: c# wpf

我正在尝试使用TextBox样式的附加属性来启用水印文本。但是,我似乎无法让它发挥作用。该项目建设良好,但水印没有显示。有人有主意吗?感谢。

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>

2 个答案:

答案 0 :(得分:1)

要为template textbox创建一个watermark,您需要一种类似于下面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中实现了一些修改。这些更改都不会影响类的功能,但是要遵循当您想要创建一个&#34; help&#34;类时使用的命名标准。 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));
    }

WatermarkPropertyTextBoxHelper的名称已更改,因为该类有助于textbox控件。并且还将attached property的名称修改为WatermarkProperty,因此遵守所有标准命名法。

答案 1 :(得分:-1)

您不使用此类附加属性。您可以直接将它们用作普通的XAML属性:

<Label local:WatermarkProperty.Watermark="{Binding ...}" />

另外,如果你想成为一个好公民,你的WatermarkProperty应该是静态的。