.net C# PropertyGrid 显示下拉列表

PropertyGrid是.net的 一个功能非常强大的控件,最近因为项目中需要使用这个控件,特意关注了一下它的用法。

在我这个项目中主要用到的功能是编辑表的各种属性,效果如下:

显示这种效果实现思路可能不止一个,遗憾的是我目前只知道这一个,因为时间关系也没有仔细研究。我的方法是:

(1)先自定义一个控件继承CheckedListBox,代码如下:

 

代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PropertyGridDemo
{
public class CheckedListBoxUC : CheckedListBox
{
private System.Windows.Forms.Design.IWindowsFormsEditorService m_iws;

private string m_selectStr = string.Empty;

/// <summary>
/// 获取选择的字段,多个字段用"|"隔开
/// </summary>
public string SelectedFields
{
get
{
return this.m_selectStr;
}

}
public CheckedListBoxUC(System.Windows.Forms.Design.IWindowsFormsEditorService iws)
{
this.m_iws = iws;
this.Visible = true;
this.Height = 100;
this.BorderStyle = BorderStyle.None;
//添加事件
this.Leave += new EventHandler(checkedListBoxUC_Leave);

try
{
string[] strsFields = LayerParams.GetFields();
this.BeginUpdate();
this.Items.Clear();
if (null != strsFields)
{
for (int i = 0; i < strsFields.Length; i++)
{
this.Items.Add(strsFields[i]);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.EndUpdate();
}
}

void checkedListBoxUC_Leave(object sender, EventArgs e)
{
List
<string> lstStrs = new List<string>();
for (int i = 0; i < this.Items.Count; i++)
{
if (this.GetItemChecked(i))
{
lstStrs.Add((
string)this.Items[i]);
}

}
m_selectStr
= string.Join("|", lstStrs.ToArray());
this.m_iws.CloseDropDown();
}

}
}

需要注意的是必须在这个控件的构造方法里面添加好要显示的项。

(2)自定义一个类继承System.Drawing.Design.UITypeEditor

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;

namespace PropertyGridDemo
{
public class ListBoxUCConverter : System.Drawing.Design.UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
System.Windows.Forms.Design.IWindowsFormsEditorService iws
= (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
if (iws != null)
{
ListBoxUC chkListBoxUC
= new ListBoxUC(iws);
iws.DropDownControl(chkListBoxUC);
return chkListBoxUC.SelectedField;
}
return value;
}
}
}

(3)在一个类的属性中使用ListBoxConverter类

 

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace PropertyGridDemo
{
public class AtbDataRuleProps:RuleProps
{
protected string m_strFields = string.Empty;

public AtbDataRuleProps()
{ }


[CategoryAttribute(
"常规"), DescriptionAttribute("检查字段"), EditorAttribute(typeof(CheckedListBoxUCConverter), typeof(System.Drawing.Design.UITypeEditor))]
public string Fields
{
get
{
return m_strFields;
}
set
{
m_strFields
= value;
}
}
}
}

最后,不会忘记在要显示属性的相应代码里加上这句吧。propertyGrid.SelectedObject = new AtbRuleProps();

 

对于PropertyGrid这个控件,我还有很多没弄明白的,以后有空和大家共同学习!

posted on 2011-01-21 16:57  Hexy  阅读(3392)  评论(1编辑  收藏  举报

导航