How to make an auto-populated Property Readonly

Grids for WPF Forum

Posted 15 years ago by Lars Reith
Avatar
Hi,

I would like to know if there is an easy way to set an auto-populated property to readonly in the Editor.

I am using the TypeDescriptorFactory to Browse to properties of an object dynamically set to SelectedObject. I am using a DataFilter to display only the properties of interest:

<propgrid:PropertyGrid.DataFilter>
<propgrid:DataFilterGroup Operation="Or">
<propgrid:PropertyStringFilter Source="ValueName" Operation="Contains" Value="Name" />
<propgrid:PropertyStringFilter Source="ValueName" Operation="Contains" Value="Description" />
</propgrid:DataFilterGroup>
</propgrid:PropertyGrid.DataFilter>

How do I configure a property, say Name, to be readonly, meaning being just displayed, nothing more.

I figured out I can use a custom PropertyEditor with a DataTemplate simply using a TextBlock. But then I have to deal a little with styling it so everything looks like the property had been readonly by default (no setter). Can't it be done easier?

My Property Editor:

<propgrid:PropertyGrid.PropertyEditors>
<propgrid:PropertyEditor PropertyName="Name" ValueTemplate="{StaticResource SimpleTextBlock}" />
</propgrid:PropertyGrid.PropertyEditors>

The simple DataTemplate:

<DataTemplate x:Key="SimpleTextBlock">
<TextBlock>
<TextBlock.Text>
<Binding Path="Value" Mode="OneWay" RelativeSource="{RelativeSource AncestorType={x:Type Primitives:IPropertyDataAccessor}}" />
</TextBlock.Text>
</TextBlock>
</DataTemplate>




Best Regards!

Comments (1)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Lars,

It depends on how you are populating the PropertyGrid, but typically you would decorate your property with the ReadOnlyAttribute, like this:
[ReadOnly(true)]
public string Name {
    get; set;
}


Actipro Software Support

The latest build of this product (v24.1.2) was released 26 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.