只读属性网格

发布于 2024-08-22 12:19:05 字数 334 浏览 5 评论 0原文

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

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

I'm using a PropertyGrid in an application I am writing to allow users to view and sometimes edit instances of my objects. Sometimes the user may have a file open in read/write mode where they can make changes to the file through the property grid. In other cases they may have a file open in read only mode, and should not be able to make any changes to the objects through the PropetyGrid. My classes also have dynamic properties which are returned by implementing ICustomTypeDescriptor. Which is why I really want to take advantage of the built in flexibility of a PropertyGrid control.

There doesn't seem to be an easy way to set a Property-grid to a read only mode. If I disable a PropertyGrid this also prevents the user from scrolling the list. So I'm thinking the best way to do this is to add ReadOnlyAttributes to the properties at run-time. Is there some other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷雾森÷林ヴ 2024-08-29 12:19:05

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

TypeDescriptor.AddAttributes(myObject, new Attribute[]{new ReadOnlyAttribute(true)});
propertyGrid1.SelectedObject = myObject;

I have found a very quick solution for thoses who do not care about the propertygrid being grayed out.

TypeDescriptor.AddAttributes(myObject, new Attribute[]{new ReadOnlyAttribute(true)});
propertyGrid1.SelectedObject = myObject;
情深已缘浅 2024-08-29 12:19:05

由于您正在实现ICustomTypeDescriptor,因此无需添加任何属性;您只需覆盖 PropertyDescriptor 上的 IsReadOnly 即可。我认为编写一个模仿(通过 ICustomTypeDescriptor 和 TypeConverter)包装类型但始终返回只读 PropertyDescriptor 的中间类型应该非常简单> 实例?如果您想要一个示例,请告诉我(尽管它不是微不足道的)。

您可能还想检查类似的内容是否提供了构建它的功能。

Since you are implementing ICustomTypeDescriptor there is no need to add any attributes; you can just override IsReadOnly on the PropertyDescriptor. I'm thinking it should be pretty simple to write an intermediary type that mimics (via ICustomTypeDescriptor and TypeConverter) a wrapped type but always returns readonly PropertyDesciptor instances? Let me know if you want an example (it isn't trivial though).

You might also want to check whether something like this offers it built it.

天赋异禀 2024-08-29 12:19:05

我最终从 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;
            }
        }
    }
}

I ended up inheriting from PropertyGrid and select the parent category whenever a property is selected.

Simple and no need to use 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;
            }
        }
    }
}
俏︾媚 2024-08-29 12:19:05

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

My advice would be to write a custom control that inherits from the propertygrid control, and in that custom control, have a boolean value of readonly, and then override some things and check, if(readonly) then cancel the action

小鸟爱天空丶 2024-08-29 12:19:05

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

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

#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

I ran into this one. I wanted a control that was read only but not grayed out.

Inherit from the property grid control and create your own read only version by adding the following code to override key presses

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文