簡體   English   中英

在樣式中使用附加屬性

[英]Using attached properties in a style

我正在嘗試為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>

要為帶有watermarktextbox創建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));
    }

更改為TextBoxHelperWatermarkProperty的名稱是因為該類可幫助textbox控件。 並且還將attached property的名稱修改為WatermarkProperty ,因此遵循所有標准命名法。

您不會使用類似的附加屬性。 您只是直接將它們用作常規XAML屬性:

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

另外,如果您想成為好公民,您的WatermarkProperty應該是靜態的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM