webcontrolsizeconverter.cs

来自「ASP.NET服务器控件高级编程电子书」· CS 代码 · 共 79 行

CS
79
字号
using System;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI.WebControls;

namespace WroxDesign.Design
{
	public class WebControlSizeConverter : TypeConverter
	{

		public WebControlSizeConverter()
		{
		}

		private WebControlSize FromString(object value)
		{
			string[] values = ((string)value).Split(',');
			if (values.Length != 2) throw new ArgumentException("Could not convert the value");; 
			try
			{
				Unit h = new Unit(values[0]);
				Unit w = new Unit(values[1]);
				return new WebControlSize(h, w);
			}
			catch
			{
				throw new ArgumentException("Could not convert the value");
			}
		}

		private string ToString(object value)
		{
			WebControlSize size = value as WebControlSize;
			return String.Format("{0}, {1}", size.Height, size.Width);
		}

		public override bool CanConvertFrom(
			ITypeDescriptorContext context, 
			Type sourceType) 
		{
			if (sourceType == typeof(string)) return true; 
			return base.CanConvertFrom(context, sourceType);
		}

		public override object ConvertFrom(
			ITypeDescriptorContext context, 
			CultureInfo culture, object value) 
		{
			if (value is string) 
			{
				return FromString(value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override bool CanConvertTo(
			ITypeDescriptorContext context,
			Type destinationType)
		{
			if (destinationType == typeof(string)) return true;
			return base.CanConvertTo(context, destinationType);
		}

		public override object ConvertTo(
			ITypeDescriptorContext context, 
			CultureInfo culture, object value, 
			Type destinationType) 
		{  
			if (destinationType == typeof(string)) 
			{
				foreach (IComponent comp in context.Container.Components)
					if (comp is Label) ((Label)comp).Text = "WebControlSize Changed!!";
				return ToString(value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?