【问题标题】:Custom Dropdown Editor Control in PropertyGrid is displayed in WHITE colorPropertyGrid 中的自定义下拉编辑器控件以白色显示
【发布时间】:2013-06-05 01:34:13
【问题描述】:

对 PropertyGrid 控件进行了一些尝试,现在偶然发现了一个奇怪的问题。 我在一个位掩码的类中有一个 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; }

它有效,但我的控件显示为白色(包括按钮),这看起来很不对劲。我不知道为什么。这是屏幕截图的链接:that is how the control looks like in action。 有人对为什么以及如何避免它有任何想法吗?我可以调用表单,但我宁愿坚持使用下拉菜单。

提前致谢!

【问题讨论】:

    标签: controls custom-controls propertygrid


    【解决方案1】:

    属性网格使用ViewBackColor 属性来显示下拉背景颜色。我没有看到任何其他允许更改下拉背景颜色的属性。

    但是,下拉列表中显示的控件是您可以修改的控件(表单)的父级,代码如下:

    public partial class MaskEditorControl : UserControl, IIntegerMaskControl
    {
        private Color _initialBackColor;
    
        public MaskEditorControl()
        {
            InitializeComponent();
            _initialBackColor = BackColor;
        }
    
        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (Parent != null)
            {
                Parent.BackColor = _initialBackColor;
            }
        }
    }
    

    【讨论】:

    • 无法对该答案进行投票(没有足够的声誉),但它有效。谢谢!有没有办法告诉控件不要选择父母的背景颜色?那将是最好的方法。
    • 我不这么认为,下拉保持器表单(父级)使用视图颜色,我认为这是设计使然,因此看起来更好 - 从属性网格创建者的角度来看 :-) .
    猜你喜欢
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多