为Windows窗体实现TypeConverter我想为自定义字体粗细实现TypeConverter。我查看了Microsoft发布的类型转换器,如SizeConverter。当我在Thickness属性上键入一个字符串或更改其中一个属性时,设计器会使用它,但它不会将更改保存到“Designer.cs”。它必须是从'Thickness'类型到'InstanceDescriptor'的转换,但我发现我的代码没有任何问题……这是Thickness:[TypeConverter(typeof(ThicknessConverter))]publicstructThickness{Doubletop;双底;双权;双左;publicThickness(DoubleuniformLength){top=uniformLength;底部=统一长度;右=统一长度;左=统一长度;}publicThickness(双左,双顶,双右,双底){this.left=left;this.top=top;this.right=正确;这个。底部=底部;}publicDoubleTop{get{returntop;}设置{顶部=值;}}publicDoubleBottom{get{returnbottom;}设置{底部=值;}}publicDoubleRight{get{返回权;}设置{右=值;}}publicDoubleLeft{get{返回左;}设置{左=值;}}publicDoubleUniformLength{get{if(!IsUniform)thrownewInvalidOperationException();否则返回顶部;}设置{顶部=值;底部=价值;正确=价值;底部=v值;}}publicBooleanIsUniform{get{returntop==bottom&&bottom==right&&bottom==left;}}}这是类型转换器:publicclassThicknessConverter:TypeConverter{publicThicknessConverter(){}publicoverrideBooleanCanConvertTo(ITypeDescriptorContextcontext,TypedestinationType){returndestinationType==typeof(String)||destinationType==typeof(InstanceDescriptor);}publicoverrideObjectConvertTo(ITypeDescriptorContextcontext,System.Globalization.CultureInfoculture,Objectvalue,TypedestinationType){Thicknessthickness=(Thickness)value;if(destinationType==typeof(String)){if(thickness.IsUniform)returnthickness.Right.ToString();否则返回thickness.Left+","+thickness.Top+","+thickness.Right+","+thickness.Bottom;}elseif(destinationType==typeof(InstanceDescriptor)){if(thickness.IsUniform)returnnewInstanceDescriptor(typeof(Thickness).GetConstructor(newType[]{typeof(Double)}),newObject[]{thickness.UniformLength});else{ConstructorInfoconstructor=typeof(Thickness).GetConstructor(newType[]{typeof(Double),typeof(Double),typeof(Double),typeof(Double)});returnnewInstanceDescriptor(constructor,newObject[]{thickness.Left,thickness.Top,thickness.Right,thickness.Bottom});}}否则返回空值;}publicoverrideBooleanCanConvertFrom(ITypeDescriptorContextcontext,TypesourceType){returnsourceType==typeof(String);}publicoverrideObjectConvertFrom(ITypeDescriptorContextcontext,System.Globalization.CultureInfoculture,Objectvalue){if(valueisString){StringstringValue=(String)value;如果(stringValue.Contains(",")){String[]stringValues=stringValue.Split(',');双[]值=新的双[字符串值.长度];如果(values.Length==4){try{for(Int32i=0;i<4;i++)values[i]=Double.Parse(stringValues[i]);}catch(Exception){returnnewThickness();}返回新厚度(值[0],值[1],值[2],值[3]);}否则返回新的厚度();}else{try{returnnewThickness(Double.Parse(stringValue));}catch(Exception){returnnewThickness();}}}elsereturnbase.ConvertFrom(context,culture,value);}publicoverrideBooleanGetCreateInstanceSupported(ITypeDescriptorContextcontext){returntrue;}publicoverrideObjectCreateInstance(ITypeDescriptorContextcontext,System.Collections.IDictionarypropertyValues){returnnewThickness((Double)propertyValues["Left"],(Double)propertyValues["Top"],(Double)propertyValues["Right"],(Double)propertyValues["Bottom"]);}publicoverrideBooleanGetPropertiesSupported(ITypeDescriptorContextcontext){returntrue;}publicoverridePropertyDescriptorCollectionGetProperties(ITypeDescriptorContextcontext,Objectvalue,Attribute[]attributes){PropertyDescriptorCollectioncollection=TypeDescriptor。获取属性(类型(厚度));collection=collection.Sort(newString[]{"Left","Top","Right","Bottom"});集合.RemoveAt(4);集合.RemoveAt(4);退货收集;我唯一能想到的是转换器是否必须与使用它的设计器代码分开组装?所以,我希望我的调查能顺利结束结果:这有点奇怪。似乎VS2008中存在一些错误。要使代码按预期工作:1.将ThicknessConverter重命名为其他名称(例如:Thickness1Converter)。2.按Shift+F6构建解决方案。您现在应该能够在设计器UI中编辑Thickness属性。相应的代码将出现在*.Designer.cs文件中。之后,在某些情况下,可以将其重命名为ThicknessConverter而不会出错。但是抱歉,我无法发现规则。如果有什么不够清楚,请不要犹豫,问我。祝你好运。这对我来说很好。以下是我遵循的步骤:启动VS2010。创建一个新的WinForms项目(.NETFramework4客户端配置文件)。复制粘贴Thickness和ThicknessConverter类。创建一个新的用户控件UserControl1。向用户控件添加一个属性:publicThicknessThickness{get;放;}publicThicknessThickness{get;放;}构建项目。打开Form1设计器。将UserControl1放在Form1上。选择用户控件1。编辑厚度属性。节省。在Form1.Designer.cs中验证厚度是否正确。如果这对您不起作用,则您要么在项目中做其他事情,要么我们的设置不同。您能否提供详细信息(例如.NET版本)?以上就是C#学习教程:ImplementingTypeConverterforWindowsForms全部内容分享。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权请点击右侧联系管理员删除。如需转载请注明出处:
