更改propertygrid中只读属性的前景色

我正在使用WinForms属性网格来显示对象的属性。 但是,大多数属性都是只读的,因此显示为灰色而不是黑色。 有没有办法自定义使用的颜色? 我希望禁用的属性更容易阅读。

顺便说一句:我认为这个问题的答案可能与我想要做的事情有关。 但我不确定如何访问ControlPaint.DrawStringDisabled

不幸的是,没有内置的方法来改变颜色。 与许多标准的.NET控件一样,它们只是它们的COM等价物的包装版本。

这在实践中意味着很多,如果不是所有的绘画都由COM组件完成,那么如果你尝试重写.NET OnPaint方法并调用ControlPaint.DrawStringDisabled或任何其他绘画代码,那么很可能会有不良影响,或没有任何影响。

你的选择是:

  1. 从头开始构建自定义控件(可能是最简单的)
  2. 覆盖WndProc并尝试拦截绘制消息(不保证工作)
  3. 尝试覆盖OnPaint并在顶部进行绘画(不保证可以正常工作)

对不起,这可能不是你想要的答案,但我想不出更简单的方法。 我从苦涩的经历中知道,这种事情很难修改。

这个问题有一个简单的解决方案

只需减少RGB中的R为PropertyGrid的forcolor,如下所示:

 Me.PropertyGrid2.ViewForeColor = Color.FromArgb(1, 0, 0) 

此function仅适用于黑色。

这是什么巫术? +1! 我见过其他陷阱鼠标和键盘的解决方案。 这是迄今为止最好,最简单的解决方案。 这是我inheritance的只读控件的片段。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.ComponentModel; using System.Drawing; namespace MyCustomControls { public sealed class ReadOnlyPropertyGrid : System.Windows.Forms.PropertyGrid { #region Non-greyed read only support public ReadOnlyPropertyGrid() { this.ViewForeColor = Color.FromArgb(1, 0, 0); } //--- private bool _readOnly; public bool ReadOnly { get { return _readOnly; } set { _readOnly = value; this.SetObjectAsReadOnly(this.SelectedObject, _readOnly); } } //--- protected override void OnSelectedObjectsChanged(EventArgs e) { this.SetObjectAsReadOnly(this.SelectedObject, this._readOnly); base.OnSelectedObjectsChanged(e); } //--- private void SetObjectAsReadOnly(object selectedObject, bool isReadOnly) { if (this.SelectedObject != null) { TypeDescriptor.AddAttributes(this.SelectedObject, new Attribute[] { new ReadOnlyAttribute(_readOnly) }); this.Refresh(); } } //--- #endregion } }