这是propertyGridControl对应的对象
public class MemberLevelSelect : TacticMemberSelectBase
{
public MemberLevelSelect()
{
this.tacticType = TacticTypeEnum.Target;
}
public override string ToString()
{
return "会员等级";
}
string presentChoose = string.Empty;
[EditorAttribute(typeof(ListBoxUCConverter), typeof(System.Drawing.Design.UITypeEditor)),
CategoryAttribute("会员等级选择"), DisplayName("会员等级"), DescriptionAttribute("会员等级")]
public string DefaultFileName
{
get { return presentChoose; }
set { presentChoose = value; }
} }
我想把等级选择这个属性在propertyGridControl中显示为CheckedListBox类型.下面代码是ListBoxUCConverter的实现
public class CheckedListBoxUC : DevExpress.XtraEditors.CheckedComboBoxEdit
{
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.Leave += new EventHandler(checkedListBoxUC_Leave); try
{
this.Properties.Items.AddRange(new DevExpress.XtraEditors.Controls.CheckedListBoxItem[] {
new DevExpress.XtraEditors.Controls.CheckedListBoxItem(null, "1212"),
new DevExpress.XtraEditors.Controls.CheckedListBoxItem(null, "21234")});
}
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.Properties.Items.Count; i++)
{
if (this.Properties.Items[i].CheckState == CheckState.Unchecked)
{
lstStrs.Add(this.Properties.Items[i].ToString());
} }
m_selectStr = string.Join("|", lstStrs.ToArray());
this.m_iws.CloseDropDown();
} }
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)
{
IWindowsFormsEditorService iws = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (iws != null)
{
CheckedListBoxUC chkListBoxUC = new CheckedListBoxUC(iws);
iws.DropDownControl(chkListBoxUC);
return chkListBoxUC.SelectedFields;
}
return value;
}
}但是结果时,我点下拉框时,下拉框闪一下就没了.求大侠帮忙
public class MemberLevelSelect : TacticMemberSelectBase
{
public MemberLevelSelect()
{
this.tacticType = TacticTypeEnum.Target;
}
public override string ToString()
{
return "会员等级";
}
string presentChoose = string.Empty;
[EditorAttribute(typeof(ListBoxUCConverter), typeof(System.Drawing.Design.UITypeEditor)),
CategoryAttribute("会员等级选择"), DisplayName("会员等级"), DescriptionAttribute("会员等级")]
public string DefaultFileName
{
get { return presentChoose; }
set { presentChoose = value; }
} }
我想把等级选择这个属性在propertyGridControl中显示为CheckedListBox类型.下面代码是ListBoxUCConverter的实现
public class CheckedListBoxUC : DevExpress.XtraEditors.CheckedComboBoxEdit
{
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.Leave += new EventHandler(checkedListBoxUC_Leave); try
{
this.Properties.Items.AddRange(new DevExpress.XtraEditors.Controls.CheckedListBoxItem[] {
new DevExpress.XtraEditors.Controls.CheckedListBoxItem(null, "1212"),
new DevExpress.XtraEditors.Controls.CheckedListBoxItem(null, "21234")});
}
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.Properties.Items.Count; i++)
{
if (this.Properties.Items[i].CheckState == CheckState.Unchecked)
{
lstStrs.Add(this.Properties.Items[i].ToString());
} }
m_selectStr = string.Join("|", lstStrs.ToArray());
this.m_iws.CloseDropDown();
} }
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)
{
IWindowsFormsEditorService iws = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (iws != null)
{
CheckedListBoxUC chkListBoxUC = new CheckedListBoxUC(iws);
iws.DropDownControl(chkListBoxUC);
return chkListBoxUC.SelectedFields;
}
return value;
}
}但是结果时,我点下拉框时,下拉框闪一下就没了.求大侠帮忙
其实,checkedListBoxUC_Leave 这个方法你完全可以去掉,而在最后的 EditValue 方法return 时,join那个字符串,这样更合理些。
"我怎么去取CheckedListBoxUC里的值呢?CheckedListBoxUC在ListBoxUCConverter里,而ListBoxUCConverter却又在属性里.况且CheckedListBoxUC我是想存对象的"
这个值在ListBoxUCConverter,关闭时,会填在 propertyGrid 的编辑框上;还是那句话:
其实,checkedListBoxUC_Leave 这个方法你完全可以去掉,而在最后的 EditValue 方法return 时,join那个字符串,这样更合理些。
我想把ListBoxUCConverter 这个类写成通用的类,也就要把CheckedListBoxUC的构造方法的这句
this.Properties.Items.AddRange(new DevExpress.XtraEditors.Controls.CheckedListBoxItem[] {
new DevExpress.XtraEditors.Controls.CheckedListBoxItem(null, "1212"),
new DevExpress.XtraEditors.Controls.CheckedListBoxItem(null, "21234")});
}
改为动态加载.
但使用ListBoxUCConverter 这个属性时
[EditorAttribute(typeof(ListBoxUCConverter), typeof(System.Drawing.Design.UITypeEditor))]
这句话根本没地方传递参数啊,也就没办法向CheckedListBoxUC里传递参数.
怎么解决呢?不至于一个下拉列表定义一个编辑器吧.
谢谢了大神,结贴时一定给你多加分~
{
IWindowsFormsEditorService iws = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (iws != null)
{
CheckedListBoxUC chkListBoxUC = new CheckedListBoxUC(iws); switch (context.PropertyDescriptor.Category)
{
case "会员等级选择":
//给chkListBoxUC加相关项
break;
case"其它":
break;
}
iws.DropDownControl(chkListBoxUC);
return chkListBoxUC.SelectedFields;
}
return value;
}
因为我也需要在propertyGridControl中将性别显示成下拉的男或女。
不过我只是学习阶段,大学生一名,望多多指教啊。
对象属性:
[EditorAttribute(typeof(ListBoxUCConverter), typeof(System.Drawing.Design.UITypeEditor)),
CategoryAttribute("设置"), DisplayName(CategoryDisplayEnum.MemberLevelSelect), DescriptionAttribute("会员等级")]
public TacticMemberDetailArrayList MemberLevelChoose
{
get { return memberLevelChoose; }
set { memberLevelChoose = value; }
}
属性转换器
public class ListBoxUCConverter : System.Drawing.Design.UITypeEditor
{
private List<TacticMemberDetailBase> tacticMemberDetailList = new List<TacticMemberDetailBase>();
public ListBoxUCConverter()
{
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
} public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService iws = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (iws != null)
{ PropertyChoose propertyChoose = new PropertyChoose(iws);
this.QueryCommonData(context.PropertyDescriptor.DisplayName, propertyChoose,value); iws.DropDownControl(propertyChoose); return propertyChoose.TacticMemberDetailLst;
}
return value;
}
其中PropertyChoose是下拉后显示的控件.
[Serializable]
public partial class PropertyChoose : BaseUserControl
{
public PropertyChoose(System.Windows.Forms.Design.IWindowsFormsEditorService iws)
{
InitializeComponent();
this.m_iws = iws;
this.Visible = true; //添加事件
this.Leave += new EventHandler(CheckedListBoxUC_Leave);
}
private System.Windows.Forms.Design.IWindowsFormsEditorService m_iws; private TacticMemberDetailArrayList tacticMemberDetailLst = new TacticMemberDetailArrayList(); private DataSet ds = new DataSet(); private string displayEnum; /// <summary>
/// 获取选择的字段,多个字段用"|"隔开
/// </summary>
public TacticMemberDetailArrayList TacticMemberDetailLst
{
get
{
return this.tacticMemberDetailLst;
}
} private void CheckedListBoxUC_Leave(object sender, EventArgs e)
{
tacticMemberDetailLst.Clear();
this.gridView2.CloseEditor();
if (ds == null || ds.Tables.Count == 0)
{
return;
}
foreach (DataRow dr in this.ds.Tables[0].Rows)
{
switch (displayEnum)
{
case CategoryDisplayEnum.MemberLevelSelect:
{
if (!(bool)dr["Chk"]) break;
if ((bool)dr["Chk"])
{
MemberLevel p = new MemberLevel(dr["Name"].ToString(), dr["Id"].ToString());
tacticMemberDetailLst.Add(p);
}
break;
}