C# 如何在水印文本框上设置水印文本

C# 如何在水印文本框上设置水印文本,c#,.net,wpf,silverlight,C#,.net,Wpf,Silverlight,我有一个这样的模板 作为WPF中的水印文本框,这似乎可以很好地工作,但是如何更改水印文本 上面的代码硬编码为Text='entertexthere' 如果我像这样使用上面的 我实际上无法设置水印文本是什么 想法?使用附加的依赖属性: public static class Watermark { public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterA

我有一个这样的模板


作为WPF中的水印文本框,这似乎可以很好地工作,但是如何更改水印文本

上面的代码硬编码为Text='entertexthere'

如果我像这样使用上面的


我实际上无法设置水印文本是什么


想法?

使用附加的依赖属性:

public static class Watermark
{
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.RegisterAttached( "Text",
                                             typeof(Boolean),
                                             typeof(Watermark),
                                             new FrameworkPropertyMetadata() );

    public static void SetText( UIElement element, Boolean value )
    {
        element.SetValue( TextProperty, value );
    }

    public static Boolean GetText( UIElement element )
    {
        return (Boolean)element.GetValue( TextProperty );
    }
}
然后,为了便于控制,您可以执行以下操作:


然后,您的样式需要绑定到DP:


使用附加的依赖项属性:

public static class Watermark
{
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.RegisterAttached( "Text",
                                             typeof(Boolean),
                                             typeof(Watermark),
                                             new FrameworkPropertyMetadata() );

    public static void SetText( UIElement element, Boolean value )
    {
        element.SetValue( TextProperty, value );
    }

    public static Boolean GetText( UIElement element )
    {
        return (Boolean)element.GetValue( TextProperty );
    }
}
然后,为了便于控制,您可以执行以下操作:


然后,您的样式需要绑定到DP: