silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了.net – 如何在Silverlight 4中使用TextBox.Watermark?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在浏览MSDN文档时,您可能会遇到这样的gem:TextBox.Watermark. “太棒了!我一直想要一种内置的方法在我的文本框上做水印!这很棒,让我继续在XAML中设置它!” <TextBox Watermark="This is my watermark" Margin="20"></TextBox> 不幸的是,如果你运行它,你将得不到你所期望的: 细节: 这是什么?好吧,仔细查看MSDN
在浏览MSDN文档时,您可能会遇到这样的gem:TextBox.Watermark.

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

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

不幸的是,如果你运行它,你将得不到你所期望的:

细节:

这是什么?好吧,仔细查看MSDN文档:

那就对了.它在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属性的控件.
对我来说,似乎这是微软阻止人们在他们自己的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?全部内容,希望文章能够帮你解决.net – 如何在Silverlight 4中使用TextBox.Watermark?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。