实现自定义CollectionEditor的Propertygrid的重置功能
2015-09-18 17:36
671 查看
public class CustomCollectionEditor : CollectionEditor
{
/// <summary>
///
/// </summary>
private TreeViewControlViewModel tvcroot;
/// <summary>
///
/// </summary>
/// <param name="type"></param>
public CustomCollectionEditor ( Type type )
: base ( type )
{
}
/// <summary>
/// 限制一次选一个实例
/// </summary>
/// <returns></returns>
protected override bool CanSelectMultipleInstances ( )
{
return false;
}
PropertyGrid grid;
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override CollectionEditor.CollectionForm CreateCollectionForm ( )
{
CollectionEditor.CollectionForm oForm;
Type oType ;
FieldInfo oFieldInfo ;
System.Windows.Forms.PropertyGrid oPropertyGrid ;
oForm = base.CreateCollectionForm();
oType = oForm.GetType();
oFieldInfo = oType.GetField("propertyBrowser", BindingFlags.NonPublic|BindingFlags.Instance);
if ( oFieldInfo != null )
{
//'取得属性窗口控件
oPropertyGrid = oFieldInfo.GetValue ( oForm ) as System.Windows.Forms.PropertyGrid;
//'设定属性窗口控件的[说明]区域为可视
ContextMenuStrip mContextMenuStrip = new ContextMenuStrip ( );
mContextMenuStrip.Items.Add ( SR.GetString ( "Reset" ) );
mContextMenuStrip.Items[0].Click += new EventHandler ( CustomCollectionEditor_Click );
oPropertyGrid.ContextMenuStrip = mContextMenuStrip;
grid = oPropertyGrid;
}
return oForm;
}
/// <summary>
/// 指定创建的对象类型
/// </summary>
/// <returns></returns>
protected override Type CreateCollectionItemType ( )
{
return typeof ( TreeNodeViewModel );
}
/// <summary>
///
/// </summary>
/// <param name="itemType"></param>
/// <returns></returns>
protected override object CreateInstance ( Type itemType )
{
//创建一个实例
TreeNodeViewModel mTreeNodeViewModel = GetTreeNodeViewModel ( Context );
object[] parameters = new object[2];
parameters[0] = tvcroot;
parameters[1] = mTreeNodeViewModel;
TreeNodeViewModel o = ( TreeNodeViewModel ) itemType.Assembly.CreateInstance ( itemType.FullName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null );
//IDesignerHost host = ( IDesignerHost ) this.GetService ( typeof ( IDesignerHost ) );
//host.Container.Add ( o );//重要!自动生成组件的设计时代码!
DelegateTreeNodeViewModel delegateNode = new DelegateTreeNodeViewModel ( o );
return delegateNode;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected TreeNodeViewModel GetTreeNodeViewModel ( System.ComponentModel.ITypeDescriptorContext context )
{
TreeNodeViewModel nodeVM = null;
if ( ( context != null ) && ( context.Instance != null ) )
{
if ( context.Instance is TreeNodeViewModel )
{
nodeVM = ( context.Instance as TreeNodeViewModel );
}
}
return nodeVM;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected TreeViewControlViewModel GetTreeNodeControlViewModel ( System.ComponentModel.ITypeDescriptorContext context )
{
TreeViewControlViewModel ControlVM = null;
if ( ( context != null ) && ( context.Instance != null ) )
{
if ( context.Instance is NormalControlPropertyDescriptor )
{
NormalControlPropertyDescriptor descrptor = context.Instance as NormalControlPropertyDescriptor;
if ( ( descrptor != null ) && ( descrptor.DelegateShape != null ) )
{
WpfSurrogate surrogate = descrptor.DelegateShape as WpfSurrogate;
if ( ( surrogate != null ) && ( surrogate.InnerElement != null ) )
{
if ( surrogate.InnerElement is TreeViewControlViewModel )
ControlVM = surrogate.InnerElement as TreeViewControlViewModel;
}
}
}
else if ( context.Instance is TreeNodeViewModel )
{
ControlVM = ( context.Instance as TreeNodeViewModel ).Root;
}
return ControlVM;
}
return ControlVM;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="provider"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object EditValue ( ITypeDescriptorContext context, IServiceProvider provider, object value )
{
if ( tvcroot == null )
tvcroot = GetTreeNodeControlViewModel ( context );
ObservableCollection<DelegateTreeNodeViewModel> editValue=new ObservableCollection<DelegateTreeNodeViewModel>();
var collection=value as ObservableCollection<TreeNodeViewModel>;
if ( collection != null )
{
collection.ForEach ( x => editValue.Add ( new DelegateTreeNodeViewModel ( x ) ) );
}
ObservableCollection<DelegateTreeNodeViewModel> editResult = base.EditValue ( context, provider, editValue ) as ObservableCollection<DelegateTreeNodeViewModel>;
if ( editResult != null )
{
ObservableCollection<TreeNodeViewModel> result = new ObservableCollection<TreeNodeViewModel> ( );
editResult.ForEach ( x => result.Add ( x.SelectObject ) );
return result;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="instance"></param>
protected override void DestroyInstance ( object instance )
{
base.DestroyInstance ( instance );//重要!自动删除组件的设计时代码!
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override UITypeEditorEditStyle GetEditStyle ( ITypeDescriptorContext context )
{
if ( context != null && context.Instance != null )
{
return UITypeEditorEditStyle.Modal;
}
return base.GetEditStyle ( context );
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void CustomCollectionEditor_Click ( object sender, EventArgs e )
{
if ( grid != null )
grid.ResetSelectedProperty ( );
}
}
public class CustomCollectionEditor : CollectionEditor
{
/// <summary>
///
/// </summary>
private TreeViewControlViewModel tvcroot;
/// <summary>
///
/// </summary>
/// <param name="type"></param>
public CustomCollectionEditor ( Type type )
: base ( type )
{
}
/// <summary>
/// 限制一次选一个实例
/// </summary>
/// <returns></returns>
protected override bool CanSelectMultipleInstances ( )
{
return false;
}
PropertyGrid grid;
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override CollectionEditor.CollectionForm CreateCollectionForm ( )
{
CollectionEditor.CollectionForm oForm;
Type oType ;
FieldInfo oFieldInfo ;
System.Windows.Forms.PropertyGrid oPropertyGrid ;
oForm = base.CreateCollectionForm();
oType = oForm.GetType();
oFieldInfo = oType.GetField("propertyBrowser", BindingFlags.NonPublic|BindingFlags.Instance);
if ( oFieldInfo != null )
{
//'取得属性窗口控件
oPropertyGrid = oFieldInfo.GetValue ( oForm ) as System.Windows.Forms.PropertyGrid;
//'设定属性窗口控件的[说明]区域为可视
ContextMenuStrip mContextMenuStrip = new ContextMenuStrip ( );
mContextMenuStrip.Items.Add ( SR.GetString ( "Reset" ) );
mContextMenuStrip.Items[0].Click += new EventHandler ( CustomCollectionEditor_Click );
oPropertyGrid.ContextMenuStrip = mContextMenuStrip;
grid = oPropertyGrid;
}
return oForm;
}
/// <summary>
/// 指定创建的对象类型
/// </summary>
/// <returns></returns>
protected override Type CreateCollectionItemType ( )
{
return typeof ( TreeNodeViewModel );
}
/// <summary>
///
/// </summary>
/// <param name="itemType"></param>
/// <returns></returns>
protected override object CreateInstance ( Type itemType )
{
//创建一个实例
TreeNodeViewModel mTreeNodeViewModel = GetTreeNodeViewModel ( Context );
object[] parameters = new object[2];
parameters[0] = tvcroot;
parameters[1] = mTreeNodeViewModel;
TreeNodeViewModel o = ( TreeNodeViewModel ) itemType.Assembly.CreateInstance ( itemType.FullName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null );
//IDesignerHost host = ( IDesignerHost ) this.GetService ( typeof ( IDesignerHost ) );
//host.Container.Add ( o );//重要!自动生成组件的设计时代码!
DelegateTreeNodeViewModel delegateNode = new DelegateTreeNodeViewModel ( o );
return delegateNode;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected TreeNodeViewModel GetTreeNodeViewModel ( System.ComponentModel.ITypeDescriptorContext context )
{
TreeNodeViewModel nodeVM = null;
if ( ( context != null ) && ( context.Instance != null ) )
{
if ( context.Instance is TreeNodeViewModel )
{
nodeVM = ( context.Instance as TreeNodeViewModel );
}
}
return nodeVM;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected TreeViewControlViewModel GetTreeNodeControlViewModel ( System.ComponentModel.ITypeDescriptorContext context )
{
TreeViewControlViewModel ControlVM = null;
if ( ( context != null ) && ( context.Instance != null ) )
{
if ( context.Instance is NormalControlPropertyDescriptor )
{
NormalControlPropertyDescriptor descrptor = context.Instance as NormalControlPropertyDescriptor;
if ( ( descrptor != null ) && ( descrptor.DelegateShape != null ) )
{
WpfSurrogate surrogate = descrptor.DelegateShape as WpfSurrogate;
if ( ( surrogate != null ) && ( surrogate.InnerElement != null ) )
{
if ( surrogate.InnerElement is TreeViewControlViewModel )
ControlVM = surrogate.InnerElement as TreeViewControlViewModel;
}
}
}
else if ( context.Instance is TreeNodeViewModel )
{
ControlVM = ( context.Instance as TreeNodeViewModel ).Root;
}
return ControlVM;
}
return ControlVM;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="provider"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object EditValue ( ITypeDescriptorContext context, IServiceProvider provider, object value )
{
if ( tvcroot == null )
tvcroot = GetTreeNodeControlViewModel ( context );
ObservableCollection<DelegateTreeNodeViewModel> editValue=new ObservableCollection<DelegateTreeNodeViewModel>();
var collection=value as ObservableCollection<TreeNodeViewModel>;
if ( collection != null )
{
collection.ForEach ( x => editValue.Add ( new DelegateTreeNodeViewModel ( x ) ) );
}
ObservableCollection<DelegateTreeNodeViewModel> editResult = base.EditValue ( context, provider, editValue ) as ObservableCollection<DelegateTreeNodeViewModel>;
if ( editResult != null )
{
ObservableCollection<TreeNodeViewModel> result = new ObservableCollection<TreeNodeViewModel> ( );
editResult.ForEach ( x => result.Add ( x.SelectObject ) );
return result;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="instance"></param>
protected override void DestroyInstance ( object instance )
{
base.DestroyInstance ( instance );//重要!自动删除组件的设计时代码!
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override UITypeEditorEditStyle GetEditStyle ( ITypeDescriptorContext context )
{
if ( context != null && context.Instance != null )
{
return UITypeEditorEditStyle.Modal;
}
return base.GetEditStyle ( context );
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void CustomCollectionEditor_Click ( object sender, EventArgs e )
{
if ( grid != null )
grid.ResetSelectedProperty ( );
}
}
相关文章推荐
- linux tail -f catalina.out查看控制台失败
- Repeater中绑定DropDownList
- gcc 和 arm-linux-gcc 编译器的默认搜索头文件路径
- QT5.5.0 VS2013/CentOS 7自编译
- linux下编译安装ffmpeg
- linux下分卷压缩,合并解压的3种方法
- OpenDaylight Lithium 在ubunbtu下的安装
- Linux的启动过程
- NSTimer与NSRunLoop的关系分析
- iptables 的防火墙正常运行时间和安全性 使用此高效应用程序设置并维护 Linux 防火墙
- (转)ecshop 后台商品分类添加图片的功能
- Hadoop 2.0中用户安全伪装/模仿机制实现原理
- 新浪、万网前系统架构师高俊峰:统一监控报警平台架构设计思路
- linux c 使用正则表达式
- Nginx+Keepalived搭建高可用负载均衡集群
- Linux永久关闭防火墙 vsftp 开机自启动
- PHPCMS搭建wap手机网站
- Linux chattr和lsattr
- MySQL学习5_CentOS下Mysql数据库(不使用yum命令)的安装与配置
- hadoop+maven工程伪分布下实例运行