【问题标题】:Combining datagridview with propertygrid hiding duplicate values将datagridview与propertygrid结合起来隐藏重复值
【发布时间】:2016-11-21 14:47:53
【问题描述】:

我正在开发一个具有datagridviewpropertygrid 的小型应用程序。

在这个应用程序中有一个主对象class 和几个来自主类的derived classes

例如,让我们调用MainClassDerivedClass

datagridview 绑定到BindingList(Of MainClass),当用户选择单元格或行时,propertygird 应显示DerivedClass properties

我可以做到这一点,但是因为我的 MainClass 具有在 DerivedClass 中也可用的属性,所以我有重复的值,因为我只想查看仅在 DerivedClass 中可用的属性.

我怎样才能做到这一点?

解决方案可能是 post,但遗憾的是 c# 对我来说完全是胡言乱语(我不是经验丰富的程序员..)

Public Class MainClass

    Public Property ComponentType As BodyComponentTypeEnum
    Public Enum BodyComponentTypeEnum
        Cylinder
    End Enum

    Public Property Height As Double
    Public Property Thickness As Double
    Public Property Material As String
    Public Property Diameter As Double
    Public Property Mass As Double
End Class


Public Class DerivedClass

    Inherits MainClass

    Public Property Segments As Integer
    Public Property WeldOrientation As Double

End Class

【问题讨论】:

  • 如果您已经有了答案,为什么不先尝试转换它,然后再回来解决您遇到的任何问题?
  • 它只是创建一个自定义属性并使用它来控制通过PropertyGrid.BrowsableAttributes显示哪些道具

标签: vb.net winforms datagridview properties propertygrid


【解决方案1】:

一种方法是使用TypeConverter 来提供属性,并根据某些条件仅返回子类属性。但是属性网格包含一个BrowsableAttributes 属性,它允许您告诉它只显示那些带有传递的属性和值的属性。

链接的答案使用自定义属性,但您可以使用其他属性。这将使用CategoryAttribute

Public Class Widget
    <Category("Main")>
    Public Property Name As String
    <Category("Main")>
    Public Property ItemType As String

    Public Property Length As Double
    ...

Public Class SubWidget
    Inherits Widget

    <Category("SubWidget"), DisplayName("Weld Orientation")>
    Public Property WeldOrientation As Double

要防止SubWidget 对象显示父属性,请告诉PropertyGrid 仅显示Category 为“SubWidget”的属性:

' target attribute array
Dim attr = New Attribute() {New CategoryAttribute("SubWidget")}
' pass collection to propgrid control
propGrid.BrowsableAttributes = New AttributeCollection(attr)

您传递了一个集合,这意味着您可以有多个限定符 - 如果它们要显示,一个属性必须具有所有。使用自定义属性:

<AttributeUsage(AttributeTargets.Property)>
Public Class PropertyGridBrowsableAttribute
    Inherits Attribute

    Public Property Browsable As Boolean
    Public Sub New(b As Boolean)
        Browsable = b
    End Sub

End Class
...
<Category("SubWidget"), DisplayName("Weld Orientation"),
PropertyGridBrowsable(True)>
Public Property WeldOrientation As Double

如果存在链,如果这些(SubSubWidget 和更多)一个简单的布尔值是不够的,除非您创建多个属性以便只显示“最后一个”项目的属性。

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 2012-11-03
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多