c# - 是否可以默认隐藏类的属性,例如在 DataGridView 中?

标签 c# vb.net datagridview componentmodel

我很清楚 <System.ComponentModel.Browsable("False")>我可以应用于类的每个属性的属性。是否可以设置 Browsable 的默认值所有属性的属性为 False

下面的代码编译并说明了我想要实现的目标,但遗憾的是它没有按预期工作:

<Browsable(False)>
Public Class SomeClass

    Public Property HiddenByDefaultProperty1 As Object
    Public Property HiddenByDefaultProperty2 As Object

    ...

    <Browsable(True)> Public Property BrowsableProperty As Object

End Class

要实现我想要的,我必须申请 <Browsable(False)>我不想在我的 DataGridView 中显示的所有属性,这是一大堆代码困惑。

如果我只需要指定 <Browsable(True)> 就好了对于我想展示的属性。但是:这可能吗?

最佳答案

不,我不认为使用 browsable 属性是可能的。但是,您可以通过实现 ICustomTypeDescriptor 来控制 DataGridView 将绑定(bind)到哪些属性。界面。

型号

C#:

public class Foo : ICustomTypeDescriptor
{

    public string P1 { get; set; }
    public string P2 { get; set; }
    public string P3 { get; set; }
    public string P4 { get; set; }
    public string P5 { get; set; }
    public string P6 { get; set; }
    public string P7 { get; set; }
    public string P8 { get; set; }
    public string P9 { get; set; }

    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {

        var properties = new[] { "P1", "P3", "P7" };

        var descriptors = TypeDescriptor
            .GetProperties(typeof(Foo))
            .Cast<PropertyDescriptor>()
            .Where(p => properties.Any(s => s == p.Name))
            .ToArray();

        return new PropertyDescriptorCollection(descriptors);

    }

    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
        return new AttributeCollection(null);
    }

    string ICustomTypeDescriptor.GetClassName()
    {
        return null;
    }

    string ICustomTypeDescriptor.GetComponentName()
    {
        return null;
    }

    TypeConverter ICustomTypeDescriptor.GetConverter()
    {
        return null;
    }

    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
    {
        return null;
    }

    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
    {
        return null;
    }

    object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
    {
        return null;
    }

    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
    {
        return new EventDescriptorCollection(null);
    }

    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
    {
        return new EventDescriptorCollection(null);
    }

    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        return ((ICustomTypeDescriptor)this).GetProperties(null);
    }

    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }
}

VB.Net:

Public Class Foo
    Implements ICustomTypeDescriptor

    Public Property P1 As String
    Public Property P2 As String
    Public Property P3 As String
    Public Property P4 As String
    Public Property P5 As String
    Public Property P6 As String
    Public Property P7 As String
    Public Property P8 As String
    Public Property P9 As String

    Private Function GetProperties(attributes() As Attribute) As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties

        Dim properties = {"P1", "P3", "P7"}

        Dim descriptors = TypeDescriptor _
            .GetProperties(GetType(Foo)) _
            .Cast(Of PropertyDescriptor) _
            .Where(Function(p) properties.Any(Function(s) s = p.Name)) _
            .ToArray()

        Return New PropertyDescriptorCollection(descriptors)

    End Function

    Private Function GetAttributes() As AttributeCollection Implements ICustomTypeDescriptor.GetAttributes
        Return New AttributeCollection(Nothing)
    End Function

    Private Function GetClassName() As String Implements ICustomTypeDescriptor.GetClassName
        Return Nothing
    End Function

    Private Function GetComponentName() As String Implements ICustomTypeDescriptor.GetComponentName
        Return Nothing
    End Function

    Private Function GetConverter() As TypeConverter Implements ICustomTypeDescriptor.GetConverter
        Return Nothing
    End Function

    Private Function GetDefaultEvent() As EventDescriptor Implements ICustomTypeDescriptor.GetDefaultEvent
        Return Nothing
    End Function

    Private Function GetDefaultProperty() As PropertyDescriptor Implements ICustomTypeDescriptor.GetDefaultProperty
        Return Nothing
    End Function

    Private Function GetEditor(editorBaseType As Type) As Object Implements ICustomTypeDescriptor.GetEditor
        Return Nothing
    End Function

    Private Function GetEvents() As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
        Return New EventDescriptorCollection(Nothing)
    End Function

    Private Function GetEvents(attributes() As Attribute) As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
        Return New EventDescriptorCollection(Nothing)
    End Function

    Private Function GetProperties() As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
        Return DirectCast(Me, ICustomTypeDescriptor).GetProperties(Nothing)
    End Function

    Private Function GetPropertyOwner(pd As PropertyDescriptor) As Object Implements ICustomTypeDescriptor.GetPropertyOwner
        Return Me
    End Function

End Class

数据 GridView :

C#:

var list = new List<Foo>();

list.Add(new Foo());
list.Add(new Foo());
list.Add(new Foo());

this.dataGridView1.DataSource = list;

VB.Net:

Dim list As New List(Of Foo)

list.Add(New Foo())
list.Add(New Foo())
list.Add(New Foo())

Me.DataGridView1.DataSource = list

DataGridView

属性网格:

C#:

this.propertyGrid1.SelectedObject = new Foo();

VB.Net:

Me.PropertyGrid1.SelectedObject = New Foo()

PropertyGrid

关于c# - 是否可以默认隐藏类的属性,例如在 DataGridView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523871/

相关文章:

c# - 如何从主程序打开的winform中调用主程序?

c# - 我可以在 GET 请求中访问 querystring 变量,但不能在 POST 中访问

vb.net - 删除表单关闭按钮

c# - 将 .net Framework 项目与 .net Core 项目(和语言)混合 - System.IO.FileNotFoundException

c# - 在 datagridview 组合框中将查询结果显示为默认项

c# - 无法将 List 转换为 IList

c# - CA2000 错误,语句为 'using'。如何使这个有效?

vb.net - 将 delphi 的 System.Copy 转换为 .net

.net - WinForms - DataGridView - 未选择单元格

c# - DataGridView - 如何连接到与单元格更改关联的事件?