.net - 如何在 Silverlight 4 中使用 TextBox.Watermark?

标签 .net silverlight-4.0

在浏览 MSDN 文档时,您可能会遇到这个 gem:TextBox.Watermark。

“太棒了!我一直想要一种内置的方法来在我的文本框上添加水印!太棒了,让我继续在 XAML 中设置它!”

<TextBox Watermark="This is my watermark" Margin="20"></TextBox>

不幸的是,如果你运行这个你不会得到你期望的:

enter image description here

细节: enter image description here

这是什么?嗯,仔细看看MSDN文档: enter image description here

没错。它在 Silverlight 4 中受支持,但它也表示“请勿在 Silverlight 4 应用程序中使用”。如果您确实使用它,您会收到一个 System.NotImplemented 异常。为了验证,这里是通过 Reflector 反编译的属性代码:

[EditorBrowsable(EditorBrowsableState.Never)]
public object Watermark
{
get
{
StubHelper.ThrowIfNotInDesignMode();
return base.GetValue(WatermarkProperty);
}
set
{
StubHelper.ThrowIfNotInDesignMode();
base.SetValue(WatermarkProperty, value);
}
}

就是这样——只要不在设计模式下,它就会抛出异常。这没有意义吧?微软为什么要这样做?

不幸的是,我还没有找到任何明确的答案,但是如果我不得不猜测这是因为微软计划在未来版本(可能是 v5)中在 TextBox 控件上实现 Watermark 行为并且想要有效地保留这个属性所以第三方控件创建者不会继承 TextBox 并创建自己的 Watermark 属性。 我知道至少有一家控件供应商 ComponentOne 拥有一个继承自 TextBox 并提供 Watermark 属性的控件。 在我看来,这似乎是 Microsoft 阻止人们在他们自己的 TextBox 子类上使用此属性名称的方式。

最佳答案

创建一类图书馆项目。添加类文件使用以下代码.....之后在您的项目中添加此 dll。

public class WatermarkTextBox : TextBox 
{ 
    private bool displayWatermark = true; 
    private bool hasFocus = false; 
     public WatermarkTextBox() 
    { 
        this.GotFocus += new RoutedEventHandler(WatermarkTextBox_GotFocus); 
        this.LostFocus += new RoutedEventHandler(WatermarkTextBox_LostFocus); 
        this.TextChanged += new TextChangedEventHandler(WatermarkTextBox_TextChanged); 
        this.Unloaded += new RoutedEventHandler(WatermarkTextBox_Unloaded); 
    } 

    private void WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
        if (!hasFocus && Text == "") 
        { 
            setMode(true); 
            displayWatermark = true; 
            this.Text = Watermark; 
        } 
    } 

    private void WatermarkTextBox_Unloaded(object sender, RoutedEventArgs e) 
    { 
        this.GotFocus -= WatermarkTextBox_GotFocus; 
        this.LostFocus -= WatermarkTextBox_LostFocus; 
        this.Unloaded -= WatermarkTextBox_Unloaded; 
        this.TextChanged -= WatermarkTextBox_TextChanged; 
    } 

    private void WatermarkTextBox_GotFocus(object sender, RoutedEventArgs e) 
    { 
        hasFocus = true; 
        if (displayWatermark) 
        { 
            setMode(false); 
            this.Text = ""; 
        } 
    } 
    private void WatermarkTextBox_LostFocus(object sender, RoutedEventArgs e) 
    { 
        hasFocus = false; 
        if (this.Text == "") 
        { 
            displayWatermark = true; 
            setMode(true); 
            this.Text = Watermark; 
        } 
        else 
        { 
            displayWatermark = false; 
        } 
    } 
    private void setMode(bool watermarkStyle) 
    { 
        if (watermarkStyle) 
        { 
            this.FontStyle = FontStyles.Italic; 
            this.Foreground = new SolidColorBrush(Colors.Gray); 
        } 
        else 
        { 
            this.FontStyle = FontStyles.Normal; 
            this.Foreground = new SolidColorBrush(Colors.Black); 
        } 
    } 
    public new string Watermark 
    { 
        get { return GetValue(WatermarkProperty) as string; } 
        set { SetValue(WatermarkProperty, value); } 
    } 
    public static new readonly DependencyProperty WatermarkProperty = 
        DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(watermarkPropertyChanged)); 
    private static void watermarkPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
        WatermarkTextBox textBox = obj as WatermarkTextBox; 
        if (textBox.displayWatermark) 
        { 
            textBox.Text = e.NewValue.ToString(); 
            textBox.setMode(true); 
        } 
    } 

XAML:

  xmlns:watertext="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"


    <watertext:WatermarkTextBox Watermark="WElcome" Margin="150,115,120,166"></watertext:WatermarkTextBox>

关于.net - 如何在 Silverlight 4 中使用 TextBox.Watermark?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14009593/

相关文章:

c# - Windows Phone 7 的哪个程序集包含 EventProgress?

xaml - 银光 4.0 : DataTemplate Error

.net - Silverlight 本地化 - 如何覆盖 Windows 区域性

C# 等同于 Java 的 Exception.printStackTrace()?

c# - WindowsIdentity.GetCurrent().Impersonate() 做什么

silverlight-4.0 - Prism 复合 Material 应用 - 可混合性

silverlight-4.0 - MVVM/用户控件和 View = ViewModel 约定

c# - ListView 中的自动宽度

c# - 学习CSLA.NET框架

.net - unicode 实现 : many fonts, 还是一种大字体?