我遇到了一个PropertyGrid控件的问题,与IT技术有关。我的类中有一个Uint32属性,它是一个位掩码。因此,我决定创建一个自定义下拉用户控件,带有32个按钮,以使Uint32可编辑。以下是该类(不包括按钮单击处理程序):
class MaskEditorControl : UserControl, IIntegerMaskControl
{
public MaskEditorControl()
{
InitializeComponent();
}
public UInt32 ModifyMask(IServiceProvider provider, UInt32 mask)
{
IWindowsFormsEditorService editorService = null;
if (provider != null)
editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (editorService != null)
{
m_mask = mask;
checkBox0.CheckState = (m_mask & (1 << 0)) == 0 ? CheckState.Unchecked : CheckState.Checked;
checkBox1.CheckState = (m_mask & (1 << 1)) == 0 ? CheckState.Unchecked : CheckState.Checked;
checkBox2.CheckState = (m_mask & (1 << 2)) == 0 ? CheckState.Unchecked : CheckState.Checked;
editorService.DropDownControl(this);
}
return m_mask;
}
private UInt32 m_mask = 0;
}
ModifyMask(...)是实现了IIntegerMaskControl接口的函数,它被另一个类调用:
public interface IIntegerMaskControl
{
UInt32 ModifyMask(IServiceProvider provider, UInt32 mask);
}
public class IntegerMaskEditor : UITypeEditor
{
public static IIntegerMaskControl control = null;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (control == null)
return "Error: IIntegerMaskControl not set!";
return control.ModifyMask(provider, (UInt32)value);
}
}
这里是属性本身:
[System.ComponentModel.CategoryAttribute("Base")]
[Editor(typeof(IntegerMaskEditor), typeof(UITypeEditor))]
public UInt32 renderMask { get; set; }
它能正常工作,但我的控件显示为白色(包括按钮),看起来很奇怪。我不知道原因在哪里。这是屏幕截图的链接:这就是控件在使用时的样子。有人对此有什么想法和避免方法吗?我可以调用一个窗体,但我宁愿坚持使用下拉列表。
提前致谢!