Readonly PropertyGrid

I use PropertyGrid in the application that I am writing so that users can view and sometimes edit instances of my objects. Sometimes a user can open a file in read / write mode, where they can make changes to the file through the property grid. In other cases, they can open the file in read-only mode and should not make any changes to objects through PropetyGrid. My classes also have dynamic properties that are returned when implementing ICustomTypeDescriptor. That is why I really want to take advantage of the built-in flexibility of the PropertyGrid control.

There seems to be no easy way to set the property grid to read-only mode. If I turn off the PropertyGrid, this also prevents the user from scrolling through the list. Therefore, I think the best way to do this is to add ReadOnlyAttributes to the properties at runtime. Is there another way?

+4
source share
4 answers

Since you are implementing ICustomTypeDescriptor , there is no need to add any attributes; you can just override IsReadOnly on the PropertyDescriptor . I think it's quite simple to write an intermediate type that mimics (via ICustomTypeDescriptor and TypeConverter ) a wrapped type, but always returns readonly PropertyDesciptor instances? Let me know if you want an example (this is not just trivial).

You can also check that something like this suggests creating it.

+2
source

I found a very quick solution for those who do not care that the grid property is grayed out.

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

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

0
source

I came across this. I need a control that was read-only but not gray.

Inherit from the property network control and create your own version for reading by adding the following code to override keystrokes

 #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 
0
source

Source: https://habr.com/ru/post/1302089/


All Articles