silverlight控件使用(一)

近期研究silverlight,silverlight当前很多控件还没有,自己写时间又很长,所以找了一些第三方的控件。还有一些控件的特殊用法也一起记录在这里。

1.MaskedTextbox

silverlight 3.0里是没有MaskedTextbox,不知道4.0里有没有。在http://www.telerik.com/这个网站里找到了很多第三方控件,其中就有MaskedTextbox。购买正式版需要$999,不过好在有试用的,注册就可以下载。我是直接下载DLLs only,在工程里引入Telerik.Windows.Controls.Input.dll文件,就可以是用了。控件的名称是RadMaskedTextBox,设置Mask属性如0000-00-00就可以用来输入一个日期。

2.DatePicker

silverlight 3.0里带的DatePicker不太好用,不能在上面的输入框输入,还带个水印效果画蛇添足。同样的引入Telerik.Windows.Controls.Input.dll文件,就可以使用RadDatePicker了,除了能在文本框输入外,还能控制弹出的DatePicker。

3.TextBox进行验证

在google里搜了半天,确实找到了怎么再TextBox里进行验证,但是需要在xaml里写很多东西。把这样的xaml交给设计人员实在是灾难。在http://www.silverlightshow.net/里找到一个示例程序,不错,可以在http://validatesilverlight3.codeplex.com/下载到。

 Extensions.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
//
// SilverlightTips.com
//

namespace SilverlightApplication1
{
    public static class Extensions
    {
        /// <summary>
        /// 这个语法很奇怪,不知道是.net 几的语法
        /// </summary>
        /// <param name="frameworkElement"></param>
        /// <param name="message"></param>
        public static void SetValidation(this FrameworkElement frameworkElement, string message)
        {
            CustomValidation customValidation = new CustomValidation(message);

            Binding binding = new Binding("ValidationError")
            {
                Mode = System.Windows.Data.BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true,
                Source = customValidation
            };
            frameworkElement.SetBinding(Control.TagProperty, binding);
        }

        public static void RaiseValidationError(this FrameworkElement frameworkElement)
        {
            BindingExpression b = frameworkElement.GetBindingExpression(Control.TagProperty);

            if (b != null)
            {
                ((CustomValidation)b.DataItem).ShowErrorMessage = true;
                b.UpdateSource();
            }
        }

        public static void ClearValidationError(this FrameworkElement frameworkElement)
        {
            BindingExpression b = frameworkElement.GetBindingExpression(Control.TagProperty);

            if (b != null)
            {
                ((CustomValidation)b.DataItem).ShowErrorMessage = false;
                b.UpdateSource();
            }
        }

        public static bool IsTextValid(this string inputText)
        {
            bool isTextValid = true;

            foreach (char character in inputText)
            {
                if (char.IsWhiteSpace(character) == false)
                {
                    if (char.IsLetterOrDigit(character) == false)
                    {
                        if (CharUnicodeInfo.GetUnicodeCategory(character) != UnicodeCategory.NonSpacingMark)
                        {
                            isTextValid = false;
                            break;
                        }
                    }
                }
            }
            return isTextValid;
        }

        public static bool IsNumberValid(this string inputNumber)
        {
            bool isNumberValid = true;
            int number = -1;
            if (!Int32.TryParse(inputNumber, out number))
            {
                isNumberValid = false;
            }
            return isNumberValid;
        }

        public static bool IsEmailValid(this string inputEmail)
        {
            bool isEmailValid = true;
            string emailExpression = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
            Regex re = new Regex(emailExpression);
            if (!re.IsMatch(inputEmail))
            {
                isEmailValid = false;
            }
            return isEmailValid;
        }


    }

    public class CustomValidation
    {
        #region Private Members
        private string message;
        #endregion

        #region Properties
        public bool ShowErrorMessage
        {
            get;
            set;
        }

        public object ValidationError
        {
            get
            {
                return null;
            }
            set
            {
                if (ShowErrorMessage)
                {
                    throw new ValidationException(message);
                }
            }
        }
        #endregion

        #region Constructor
        public CustomValidation(string message)
        {
            this.message = message;
        }
        #endregion
    }
}

 

 

MainPage.xaml.cs

 

……

if (((myService.LDJQ0Entity)e.Result) == null)
            {
                //MessageBox.Show("没有");
                TextBox1.ClearValidationError();
            }
            else
            {
                //MessageBox.Show("有");
                TextBox1.SetValidation("有这个身份证号");
                TextBox1.RaiseValidationError();
            }

……

 

效果图:

 

 

未完待续……

 

posted on 2010-06-14 23:16  黄金年代  阅读(1913)  评论(0编辑  收藏  举报