创建WatermarkTextBox xaml的问题

时间:2014-01-23 22:34:20

标签: c# xaml windows-phone-8

我正在尝试在XAML中为Windows Phone App创建自定义水印文本框。我有大部分工作。如果文本框绑定到包含数据的字段,则水印文本将变为不可见。但是,如果字段开始为空,然后我在控件中输入文本,我无法将水印设置为不可见。

我正在侦听TextChanged事件,但在这种情况下,Text属性保持为空。有什么想法吗?

 public class WatermarkTextBox : TextBox
{
    public WatermarkTextBox()
    {
        this.TextChanged += WatermarkTextBox_TextChanged;

    }

    public string WatermarkText
    {
        get { return (string)this.GetValue(WatermarkTextProperty); }
        set { this.SetValue(WatermarkTextProperty, value); }
    }

    public static readonly DependencyProperty WatermarkTextProperty = DependencyProperty.Register(
      "WatermarkText", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(string.Empty));


    public Visibility WatermarkTextVisibility
    {
        get { return (Visibility)this.GetValue(WatermarkTextVisibilityProperty); }
        set { this.SetValue(WatermarkTextVisibilityProperty, value); }
    }

    public static readonly DependencyProperty WatermarkTextVisibilityProperty = DependencyProperty.Register(
      "WatermarkTextVisibility", typeof(Visibility), typeof(WatermarkTextBox), new PropertyMetadata(Visibility.Visible));



    void WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {

        this.WatermarkTextVisibility = string.IsNullOrEmpty(this.Text) ? Visibility.Visible : System.Windows.Visibility.Collapsed;
    }


}

1 个答案:

答案 0 :(得分:-1)

在NuGet上查看我的Watermark Textbox For C#/XAML WinRT Apps

如果您想要来源,请查看

WinRT XAML Toolkit

Callisto

请不要在上面链接只打开CS文件,你可能需要它的主题所以检查repo中的generic.xaml文件。