Search code examples
c#devexpresspropertygrid

Showing the unit of a property in PropertyGrid (using C#)


I am displaying a property called "Min Height" in my propertygrid. It's an int from the ViewModel class in which it is defined. However, when i show it, i want the unit 'dip' to appear next to it. As soon as the user clicks it to edit though, the 'dip' should disappear and only the int number should get edited.

Cheers!


Solution

  • DevExpress has different property grids - one for WinForms platform, another for WPF. Both of them are named PropertyGridControl, so it is difficult to guess which one you are talking about. The solution depends on the platform. I will describe the both.

    In WinForms platform DevExpress supports several Formatting Approaches that can be applied to editors or to specific elements of complex controls. For your task, it is convenient to use the approach described in this article: How to: Add Custom Text to a Formatted String. That is, you can assign the text like this to the FormatString property: "{0} dip". To assign the format to the specific row, use the BaseRow.RowProperties.Format property.

    In WPF, you can implement a custom value converter and assign it to the PropertyDefinition.EditSettings.DisplayTextConverter property. The IValueConverter.Convert method will be automatically invoked each time the property grid needs to know the display value. Put your code in this method to add an appropriate text to the actual value.

    The both approaches change the text of corresponding property grid cells only in the display mode. Once the user clicks inside the cell to invoke the property editor, the display text will be replaced with the actual value.