WinForm控件开发总结(七)-----为复杂属性的子属性提供编辑功能

      前面的几篇文章中,我们给控件添加一个复杂的类型 Scope ,并且给它的类型提供的一个类型转换器,现在我们可以在属性浏览器中编辑它的值,并且它的值也被串行化的源代码里了。但是你有没有发现,在属性浏览器里编辑这个属性的值还是不太方便。因为属性只是“ 10 , 200 ” 这种形式的,所以,你必须按照这种格式来修改,一旦格式错误就会引发异常,比如输入一个“ 10200 ” 。我们期望这个属性的每一子属性都能够被独立的编辑就好了,这并非不能实现,而且实现还很简单。
      为了在属性浏览器里能够独立的编辑子属性,我们还要重写两个方法: GetPropertiesSupported ()和 GetProperties ();下面是 ScopeConverter 的完整代码:
   
None.gif public   class  ScopeConverter : TypeConverter
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (sourceType == typeof(String)) return true;
InBlock.gif
InBlock.gif            
return base.CanConvertFrom(context, sourceType);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (destinationType == typeof(String)) return true;
InBlock.gif
InBlock.gif            
if (destinationType == typeof(InstanceDescriptor)) return true;
InBlock.gif
InBlock.gif            
return base.CanConvertTo(context, destinationType);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String result 
= "";
InBlock.gif            
if (destinationType == typeof(String))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Scope scope 
= (Scope)value;
InBlock.gif                result 
= scope.Min.ToString()+"," + scope.Max.ToString();
InBlock.gif                
return result;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (destinationType == typeof(InstanceDescriptor))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                ConstructorInfo ci 
= typeof(Scope).GetConstructor(new Type[] dot.gif{typeof(Int32),typeof(Int32) });
InBlock.gif                Scope scope 
= (Scope)value;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
return new InstanceDescriptor(ci, new object[] dot.gif{ scope.Min,scope.Max });
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return base.ConvertTo(context, culture, value, destinationType);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (value is string)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                String[] v 
= ((String)value).Split(',');
InBlock.gif                
if (v.GetLength(0!= 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new ArgumentException("Invalid parameter format");
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Scope csf 
= new Scope();
InBlock.gif                csf.Min 
= Convert.ToInt32(v[0]);
InBlock.gif                csf.Max 
= Convert.ToInt32(v[1]);
InBlock.gif                
return csf;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return base.ConvertFrom(context, culture, value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return TypeDescriptor.GetProperties(typeof(Scope), attributes);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

None.gif
      在 GetProperties 方法里,我用 TypeDescriptor 获得了 Scope 类的所有的属性描述器并返回。如果你对 TypeDescriptor 还不熟悉的话,可以参考 MSDN
      重写这两个方法并编译以后,在测试工程里查看控件的属性,你可以看到 Scope 是如下的形式:
      
      

转载于:https://www.cnblogs.com/guanjinke/archive/2006/12/15/593784.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值