您的位置:首页 > 运维架构

实现自定义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 ( );

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: