You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何无需自定义设计器隐藏TableLayoutPanel的Columns设计时属性

解决TableLayoutPanel设计时Columns属性隐藏问题(无需自定义设计器)

不用编写自定义设计器也能实现这个需求,核心思路是通过包装控件实例并实现ICustomTypeDescriptor接口,在属性获取环节过滤掉Columns属性,再将包装后的对象绑定到PropertyGrid。这样既不修改原控件或其设计器,又能精准隐藏目标属性。

步骤1:创建属性过滤包装类

这个类会包裹目标控件,并重写属性获取逻辑,过滤掉指定属性:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

public class FilteredControlWrapper : ICustomTypeDescriptor
{
    private readonly Control _targetControl;
    // 这里可以扩展要隐藏的其他属性名
    private readonly HashSet<string> _hiddenProperties = new HashSet<string> { "Columns" };

    public FilteredControlWrapper(Control target)
    {
        _targetControl = target ?? throw new ArgumentNullException(nameof(target));
    }

    // 以下方法直接委托给原控件的TypeDescriptor,保证其他属性和功能正常
    public string GetClassName() => TypeDescriptor.GetClassName(_targetControl, true);
    public string GetComponentName() => TypeDescriptor.GetComponentName(_targetControl, true);
    public TypeConverter GetConverter() => TypeDescriptor.GetConverter(_targetControl, true);
    public EventDescriptor GetDefaultEvent() => TypeDescriptor.GetDefaultEvent(_targetControl, true);
    public PropertyDescriptor GetDefaultProperty() => TypeDescriptor.GetDefaultProperty(_targetControl, true);
    public object GetEditor(Type editorBaseType) => TypeDescriptor.GetEditor(_targetControl, editorBaseType, true);
    public EventDescriptorCollection GetEvents(Attribute[] attributes) => TypeDescriptor.GetEvents(_targetControl, attributes, true);
    public EventDescriptorCollection GetEvents() => TypeDescriptor.GetEvents(_targetControl, true);
    public object GetPropertyOwner(PropertyDescriptor pd) => _targetControl;

    // 核心:过滤属性的逻辑
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        // 获取原控件的所有属性(包括设计时扩展属性)
        var originalProperties = TypeDescriptor.GetProperties(_targetControl, attributes, true);
        var filteredProperties = new List<PropertyDescriptor>();

        foreach (PropertyDescriptor pd in originalProperties)
        {
            // 排除指定的隐藏属性
            if (!_hiddenProperties.Contains(pd.Name))
            {
                filteredProperties.Add(pd);
            }
        }

        return new PropertyDescriptorCollection(filteredProperties.ToArray());
    }

    public PropertyDescriptorCollection GetProperties()
    {
        return GetProperties(null);
    }
}

步骤2:修改PropertyGrid绑定逻辑

在你获取选中组件并绑定到PropertyGrid的代码处,对TableLayoutPanel实例进行包装:

using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms;

// 假设你已经获取了选择服务和PropertyGrid实例
var selectionService = (ISelectionService)yourDesignerHost.GetService(typeof(ISelectionService));
var selectedComponents = selectionService.GetSelectedComponents().Cast<object>().ToList();
var wrappedObjects = new List<object>();

foreach (var component in selectedComponents)
{
    if (component is TableLayoutPanel tlp)
    {
        // 用包装类包裹TableLayoutPanel,过滤Columns属性
        wrappedObjects.Add(new FilteredControlWrapper(tlp));
    }
    else
    {
        // 其他控件直接添加,不影响原有功能
        wrappedObjects.Add(component);
    }
}

// 将包装后的对象绑定到PropertyGrid
yourPropertyGrid.SelectedObjects = wrappedObjects.ToArray();

原理说明

  • Columns属性是TableLayoutPanel的设计器通过扩展属性提供的,并非控件自身的CLR属性,所以直接反射GetType().GetProperties()找不到它。
  • 我们通过ICustomTypeDescriptor接管了属性的获取流程,在返回给PropertyGrid之前过滤掉了Columns属性。
  • 包装类的GetPropertyOwner方法返回原控件实例,所以其他属性的读写操作仍然会作用到真实的TableLayoutPanel上,不会影响控件的正常功能。

这个方案完全在你的运行时设计器代码层面实现,不需要修改原控件或其设计器,满足你的需求。

内容的提问来源于stack exchange,提问作者Jazimov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:05:48