I’m displaying a class with a few members (each is a class of its own) in a property grid.
I have the following situation (simplified, this is not the actual design and/or classes):
public class DataType1
{
public int Value1 { get; }
public int Value2 { get; }
}
public class DataType2
{
public int ValueA { get; }
public int ValueB { get; }
}
public class DisplayedData
{
[TypeConverter(typeof(ExpandableObjectConverter))]
[ReadOnly(true)]
public DataType1 Data1 { get; }
[TypeConverter(typeof(ExpandableObjectConverter))]
[ReadOnly(true)]
public DataType2 Data2 { get; }
}
Each member (Data1, Data2) as you can see, is displayed as an expandable object so I can see all of its members.
Now, the problem is this:
Each of these data classes is read separately from a distant source, and each read might fail (with a specific error).
I want to be able to display the composed object (DisplayedData) in the property grid, where each member is either expandable if the read was successful, or displays the error code (just a string) otherwise.
Any ideas?
I ended up creating my own TypeConverter based on ExpandableObjectConverter as Arif suggested.
My solution was somewhat simpler (I think).
I changed
DataType1andDataType2to inherit from the same base class (let’s call itBaseDataType) which holds an error code (and whether there was an error).Then in my type converter I do something like that (again, simplified):
That way, the converter itself queries the read data object and decides how to display it based on whether there was an error code.
Of course there’s more code involved (such as setting the relevant error code when needed etc.), but that’s the main idea.