2016-11-21 35 views
0

我的工作,有一个datagridviewpropertygrid一个小应用程序。与PropertyGrid中隐藏重复结合datagridview的值

在本申请中,主要对象class和几个来自主类的derived classes

因此,例如,可以让呼叫MainClassDerivedClass

datagridview绑定到BindingList(Of MainClass)和当用户选择的小区或行的propertygird应显示DerivedClassproperties

我可以能做到这一点,但因为我的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 

Application Capture

+0

如果你有答案已经,为什么不尝试将其转换,然后再与您遇到任何事后的问题回来? –

+0

它只是创建自定义属性,并使用它来控制哪些道具经由[PropertyGrid.BrowsableAttributes](https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k显示(System.Windows.Forms.PropertyGrid.BrowsableAttributes); K(TargetFrameworkMoniker-.NETFramework,版本%3Dv4.5.1); K(DevLang-VB)RD =真) – Plutonix

回答

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) 

enter image description here

你传递一个集合,其意味着你可以有多个限定符 - 一个属性必须全部显示。要使用自定义属性:

<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和更多)一个简单的布尔是不够的,除非你创建多个属性,因此只有从“最后”项目显示的属性。