如何在Silverlight 4使用TextBox.Watermark? [英] How to use TextBox.Watermark in Silverlight 4?

查看:142
本文介绍了如何在Silverlight 4使用TextBox.Watermark?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览MSDN文档,你可能会遇到这种宝石:TextBox.Watermark

While browsing MSDN documentation, you may come across this gem: TextBox.Watermark.

真棒!我一直在想一个内置的方式做水印对我的文本框!这是伟大的,让我继续前进,并设置在XAML!

"Awesome! I've been wanting a built-in way to do watermarking on my text boxes! This is great, let me go ahead and set that in XAML!"

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

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

Unfortunately, if you run this you won’t get what you expect:

和细节:

And the detail:

这是什么?好吧,看在MSDN文档紧密:

What is this? Well, look at the MSDN documentation closely:

这是正确的。它在Silverlight 4的支持,但同时也表示,不要在Silverlight 4应用程序中使用。如果你使用它,您会收到System.NotImplemented例外。为了验证,这里是code通过反射反编译属性:

That's right. It's supported in Silverlight 4, but it also says "Do not use in a Silverlight 4 application". If you do use it, you receive a System.NotImplemented exception. To verify, here is the code for the property decompiled via Reflector:

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

在那里,它是 - 任何时候它不是在设计模式,它抛出一个异常。这是没有意义的吧?为什么微软这样做?

There it is – it throws an exception any time it's not in design mode. This makes no sense right? Why would Microsoft do this?

不幸的是我没有找到任何明确的答案,但是如果要我猜这是因为微软正计划实现在未来的版本(也许V5)上的TextBox控件水印的行为,要有效地保留这个属性,以便第三方控制的创作者不继承文本框,并创建自己的水印属性。 我知道至少有一个控制供应商,ComponentOne的,谁拥有一个控制,从TextBox继承,并提供水印财产。 对我来说,看来这是对自己的文本框的子类使用此属性名劝阻人们不要微软的方式。

Unfortunately I haven't found any definitive answer yet, however if I had to guess it's because Microsoft is planning on implementing a Watermark behavior on the TextBox control in a future version (perhaps v5) and wanted to effectively reserve this property so third party control creators don't subclass TextBox and create their own Watermark property. I know of at least one control vendor, ComponentOne, who has a control that inherits from TextBox and provides a Watermark property. To me, it seems this is Microsoft's way of discouraging people from using this property name on their own TextBox subclasses.

推荐答案

创建一个类库项目。添加类文件使用下面的code .....之后添加的在这个DLL在您的项目。

Create One Class library project . Add Class File use the Following code .....After that Add The In this dll In Your Project.

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:

XAML:

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


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

这篇关于如何在Silverlight 4使用TextBox.Watermark?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆