随笔 - 44  文章 - 0 评论 - 321 trackbacks - 116
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

本博客上的所有文章如非特别说明均为原创,如果要转载请注明文章出处。

与我联系

搜索

 

常用链接

留言簿

我参加的小组

我参与的团队

随笔分类(40)

随笔档案(44)

文章分类

联系我

友情链接

积分与排名

  • 积分 - 198863
  • 排名 - 236

最新评论

阅读排行榜

评论排行榜

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

            
return base.CanConvertFrom(context, sourceType);
        }


        
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        
{
            
if (destinationType == typeof(String)) return true;

            
if (destinationType == typeof(InstanceDescriptor)) return true;

            
return base.CanConvertTo(context, destinationType);
        }


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

            }


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

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


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


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

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


        
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        
{
            
return true;
        }


        
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        
{
            
return TypeDescriptor.GetProperties(typeof(Scope), attributes);
        }

}

      在GetProperties方法里,我用TypeDescriptor获得了Scope类的所有的属性描述器并返回。如果你对TypeDescriptor还不熟悉的话,可以参考MSDN
      重写这两个方法并编译以后,在测试工程里查看控件的属性,你可以看到Scope是如下的形式:
      
      
posted on 2006-12-15 21:49 纶巾客 阅读(3563) 评论(14)  编辑 收藏 网摘 所属分类: WinForm Control

FeedBack:
#1楼 2006-12-15 22:22 yunhuasheng      
好!!
  回复  引用  查看    
#2楼 2006-12-15 22:50 Clingingboy      
顶一下
  回复  引用  查看    
#3楼 2006-12-16 10:19 river[未注册用户]
頂一下!
這些都是寫Winform控件的基礎,你的這個系列不錯!

  回复  引用    
#4楼 2006-12-16 10:39 Slash      
搞winform控件?
好,支持了
最近也想学这个
以后常来
期待更多精彩

  回复  引用  查看    
#5楼 2006-12-16 10:53 netx[未注册用户]
好东西,顶啊
  回复  引用    
#6楼[楼主] 2006-12-16 12:35 纶巾客      
谢谢大家的支持
  回复  引用  查看    
非常不错,以前一直在寻找相关内容--为复杂属性的子属性提供编辑功能 。
非常感谢。
不过引用这个代码后,发现一个问题,就是编辑子属性后,必须要再点击下父属性,其值才会更改。

  回复  引用    
还有子属性上下顺序不知道怎么改变。希望能改变成
Socpe 20,10
min 20
max 10 。


  回复  引用    
#9楼 2007-10-13 14:12 snailsun[未注册用户]
实验中经常会发生“code generation for property "scope" failed Error was "Object reference not set to an instance of an object”.关闭重启vs后就没有这个问题,但是设好scope的属性比如说“12,133”,然后再修改一次,比如说“12,144”就会报上边所说得错误。后台的代码没有生成关于Scope类的代码。
不知道有哪位大虾知道怎么回事?????

  回复  引用    
@逸云天
在你Max和Min属前上面加上定制属性NotifyParentProperty(true)
即:
[NofifyParentProperty(true)]
public int Max
{
......
}

  回复  引用    



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 593784




相关文章:

相关链接: