简体   繁体   English

在.Net,Winforms中更改只读集合时,在设计时刷新属性网格。

[英]Refresh property grid, at Design-time, when a readonly collection changes in .Net, Winforms

I have a class that has a readonly collection property - Its a list of extender providers that have been applied to the control. 我有一个具有只读集合属性的类-它是已应用于控件的扩展程序提供程序的列表。

I've implemented a simple property descriptor for the collection so that the property can be expanded in the property grid to examine each entry. 我为集合实现了一个简单的属性描述符,以便可以在属性网格中扩展属性以检查每个条目。

When I select an extender provider and set it to false, I remove it from the collection. 选择扩展程序提供程序并将其设置为false时,我将其从集合中删除。 The GetProperties method of the type converter is requeried and the property grid refreshes. 重新查询类型转换器的GetProperties方法,并刷新属性网格。

However, when I set an extender provider to true, and thus add it to the collection, GetProperties is not requeried. 但是,当我将扩展程序提供程序设置为true并将其添加到集合中时,不会重新查询GetProperties。

Somehow, the property grid is making a distinction between adding to and removing from the collection. 某种程度上,属性网格在添加和删除集合之间做出了区分。 Or alternativly, its refreshing when an extender provider is added, but not when one is removed. 或者,添加扩展程序提供程序时会刷新,但删除扩展程序时不会刷新。

How can I get the grid to refresh when I add to the collection? 添加到集合中后,如何刷新网格?

I've tried INotifyPropertyChanged and (PropertyName)Changed, but with no success. 我尝试了INotifyPropertyChanged和(PropertyName)Changed,但没有成功。

ETA: 预计到达时间:

I've knocked together a small sample to demonstate my problem. 我汇总了一个小样本来演示我的问题。 For brevity, this sample uses an array and will persist, though not correctly. 为简便起见,此示例使用数组,并且即使没有正确执行,也将保留。 This does not effect the behaviour at design time though: 但是,这不会影响设计时的行为:

Public Class MyButton
    Inherits Button

    Private _Col As String()
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property Col() As String()
        Get
            Return Me._Col
        End Get
    End Property

    Private _AlterCol As String
    ''''''<RefreshProperties(RefreshProperties.All)> _
    Public Property AlterCol() As String
        Get
            Return _AlterCol
        End Get
        Set(ByVal value As String)
            _AlterCol = value
            ReDim Preserve Me._Col(Me._Col.Length)
            Me._Col(Me._Col.Length - 1) = value
        End Set
    End Property

    Public Sub New()
        ReDim Me._Col(2)
        Me._Col(0) = "Fred"
        Me._Col(1) = "Jim"
        Me._Col(2) = "Bob"
    End Sub

End Class

Now, to see the behaviour in action, add a MyButton to a form, expand the Col property and type some text into the AlterCol property. 现在,要查看实际行为,请将MyButton添加到表单中,展开Col属性,然后在AlterCol属性中键入一些文本。

The expanded sub properties in Col will not change. Col中的扩展子属性不会更改。 However, comment out RefreshProperties.All and it will be re-queried and updated. 但是,注释掉RefreshProperties.All,它将被重新查询和更新。

My problem is that I am not updating Col from a Read/Write property. 我的问题是我没有从Read / Write属性更新Col。 I'm updating it in response to an external provider being added. 我正在更新它,以响应添加的外部提供程序。 I need to force the property grid to refresh somehow. 我需要强制属性网格以某种方式刷新。 The only way I can think of doing it is a dummy property, decorated with RefreshProperties, that I assign a changed value to. 我能想到的唯一方法是使用RefreshProperties装饰的虚拟属性,然后向其分配更改的值。

Any ideas? 有任何想法吗?

Are you trapping the INotifyPropertyChanged...upon receiving the event you may need to "disconnect" the property grid, re-set it and refresh it...or are you not trapping the 'PropertyChanged' event of the property grid? 您是否正在捕获INotifyPropertyChanged ...在接收到事件后可能需要“断开”属性网格,重新设置并刷新它...还是不是在捕获属性网格的“ PropertyChanged”事件? Am a C# guy here...so this may render useless to you...? 这里是C#家伙吗...所以这可能使您无用...?

// Within your trapping of the INotifyPropertyChanged Event Handler

    // Set 'SelectedObject' to null
    propGrid.SelectedObject = null;
    //
    propGrid.SelectedObject = this object
    //
    propGrid.Refresh();

Here's an example of trapping the Property Changed.... 这是捕获属性已更改的示例。

private void propGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) {
       // Handle the property changed for the 'propGrid' conmponent
}

Edit: As Jules has pointed out, the need to do this was at design-time, so digging around I found something that might be enlightening on CodeProject ...and here it explains how to leverage the usage of 'Extendee' used for controls...in order to get an insight into the problem. 编辑:正如Jules所指出的,这样做是在设计时进行的,因此深入研究后,我发现可能对CodeProject有所启发...在这里,它解释了如何利用控件的“扩展名”的用法 ...以深入了解问题。

It sounds like you need to make the class an extendee and bind it to the property grid at runtime... 听起来您需要使该类成为扩展对象并将其在运行时绑定到属性网格...

Have a look here also on understanding IExtenderProvider here on CodeProject also, that explains how to use a CSS stylesheet and apply it to a windows form... 在这里也可以在CodeProject上了解IExtenderProvider,它解释了如何使用CSS样式表并将其应用于Windows窗体...

Hope this helps, Best regards, Tom. 希望这对您有所帮助,汤姆,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在属性网格,.net,winforms中编辑自定义只读集合时出现问题 - Problem editing custom readonly collection in property grid, .net, winforms 在设计时更改属性属性,如ReadOnly(PropertyGrid) - Change Property Attribute Like ReadOnly at Design-Time (PropertyGrid) 如何在Winforms中为Enabled属性添加设计时支持 - How to add design-time support for Enabled property in Winforms Winforms不能在设计时设置属性值 - Winforms can't set Property-Values at design-time 在Winforms和.net中持久保存引用内部属性的集合的困难 - Difficulty with persisting a collection that references an internal property at design time in Winforms and .net WPF设计时属性 - WPF Design-time property 的WinForms。 设计时数据绑定到子控件的属性 - WinForms. Design-time Data binding to child control's property 在WinForms自定义控件中,新属性如何在设计时更改另一个属性? - In a WinForms custom control, how can a new property change another at design-time? PropertyGrid-如何在设计时增加重置集合类型属性的能力 - PropertyGrid - how to add ability to reset property of collection type in design-time 在设计时WinForms实体框架数据库连接异常 - WinForms Entity Framework database connection exception at design-time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM