8

我正在编写的应用程序中使用 PropertyGrid,以允许用户查看和有时编辑我的对象的实例。有时用户可能会以读/写模式打开文件,他们可以通过属性网格对文件进行更改。在其他情况下,他们可能会以只读模式打开文件,并且不应通过 PropetyGrid 对对象进行任何更改。我的类还具有通过实现 ICustomTypeDescriptor 返回的动态属性。这就是为什么我真的想利用 PropertyGrid 控件的内置灵活性。

似乎没有一种简单的方法可以将 Property-grid 设置为只读模式。如果我禁用 PropertyGrid,这也会阻止用户滚动列表。所以我认为最好的方法是在运行时将 ReadOnlyAttributes 添加到属性中。还有其他方法吗?

4

5 回答 5

17

对于那些不关心 propertygrid 变灰的人,我找到了一个非常快速的解决方案。

TypeDescriptor.AddAttributes(myObject, new Attribute[]{new ReadOnlyAttribute(true)});
propertyGrid1.SelectedObject = myObject;
于 2012-12-05T14:09:25.747 回答
3

由于您正在实施ICustomTypeDescriptor,因此无需添加任何属性;你可以覆盖IsReadOnly. 我认为编写一个模仿(通过和)包装类型但总是返回只读实例PropertyDescriptor的中间类型应该很简单?如果你想要一个例子,请告诉我(虽然这不是微不足道的)。ICustomTypeDescriptorTypeConverterPropertyDesciptor

您可能还想检查类似这样的东西是否提供了它。

于 2010-02-23T05:05:13.723 回答
0

我的建议是编写一个从 propertygrid 控件继承的自定义控件,并且在该自定义控件中,具有只读的布尔值,然后覆盖一些东西并检查,如果(只读)然后取消操作

于 2010-02-23T01:32:03.337 回答
0

我遇到了这个。我想要一个只读但不灰显的控件。

从属性网格控件继承并通过添加以下代码来覆盖按键创建您自己的只读版本

#Region "Non-greyed read only support"

Private isReadOnly As Boolean
Public Property [ReadOnly]() As Boolean
    Get
        Return Me.isReadOnly
    End Get
    Set(ByVal value As Boolean)
        Me.isReadOnly = value
    End Set
End Property


Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
    If Me.isReadOnly Then Return True
    Return MyBase.ProcessDialogKey(keyData)
End Function

Public Function PreFilterMessage(ByRef m As Message) As Boolean
    If m.Msg = &H204 Then 'WM_RBUTTONDOWN
        If Me.isReadOnly Then Return True
    End If
    Return False
End Function
#End Region
于 2011-04-12T19:19:22.093 回答
0

我最终从 PropertyGrid 继承并在选择属性时选择父类别。

简单且无需使用 TypeDescriptor。

public class ReadOnlyPropGrid : PropertyGrid
{
    public ReadOnlyPropGrid()
    {
        this.ToolbarVisible = false; // categories need to be always visible
    }

    protected override void OnSelectedGridItemChanged(SelectedGridItemChangedEventArgs e)
    {
        if (e.NewSelection.GridItemType == GridItemType.Property)
        {
            if (e.NewSelection.Parent != null && e.NewSelection.Parent.GridItemType == GridItemType.Category)
            {
                this.SelectedGridItem = e.NewSelection.Parent;
                return;
            }
        }
    }
}
于 2021-08-24T21:50:58.317 回答