Hi.
I have a list-based property that I want to display in the xamPropertyGrid. I want to display it as read-only, i.e. I don't want the option to add/remove items to the list using the property grid. I also want to cutomize the way the items are presented in the property grid.
I have played around with the corresponding sample (xamPropertyGrid->Display->Customizing Expandable Properties) in the Samples Browser, but the current sample does not give me exactly what I want. Attached is an annotated screenshot from the Samples Browser. I have the following questions:
1. How to configure/remove the text displayed withing the red ellipse?
2. How to remove the minus-signs displayed within the blue ellipse at the right edge?
Regarding question 2, I have tried to set the property to read-only using the ReadOnly attribute, but that does not help. If I set the property to return e.g. a IReadOnlyList the minus signs do go away. However, I then have trouble getting my type descriptors to work to e.g. modify the default item labels to someting else than '[0]', '[1]' etc.
Regards,Leif
Hi Leif,
1.) This can be changed with a TypeConverter. You can create your own custom TypeConverter and then use the TypeConverter attribute on your employees list property. In the custom TypeConverter, override the ConvertTo method and have it return the desired text.
public class MyListTypeConverter : TypeConverter { public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { return "Text Goes Here"; } } [TypeConverter(typeof(MyListTypeConverter))] public List<Employee> Employees { get; set; }
2.) Try returning a ReadOnlyCollection<T> from your property rather than just an IReadOnlyList. I used a ReadOnlyCollection<T> and the property grid got rid of the minus buttons.
Returning a ReadOnlyCollection rather than an IReadOnlyList works as expected, i.e. the minus signs are gone.
I still can't get the TypeConverter to work as expected. I already use a TypeConverter as you suggest, and as it is done in the Samples Browser sample. However, the TypeConverter seems to be applied only initially before the collection is expanded in the property grid. The Samples Broswer sample has the same issue: Initially it displays the text 'Employee's Collection', as given by the type converter. As soon as the collection is expanded, the text changes to 'List, Count: 2' and stays that way event when the collection is collapsed.
Based on the sample you referred to I take it you are using 15.2? I'm running that sample right now but I don't see the "Employee's Collection" text changing when I expand the collection to view the collection items. Adding/removing items and expanding the individual items doesn't affect this text either. What versions of .NET do you have installed on that machine?
It looks to me now that this is an issue introduced with the latest (December) 15.2 Service Release. If I revert to the previous 15.2 binaries (v15.2.20152.1000) everything works fine, i.e. the text is not changing when expanding the collection. If I use the SR binaries (v15.2.20152.2038) I get the error.
This looks to behave the same for the Samples Browser. If I run the Samples Browser from code that I compile (with the SR binaries) I get the error. If I run the installed Samples Browser executable (that uses v15.2.20152.1000 binaries from the GAC), everything works fine.
Oh! That makes a lot more sense. Normally we ship our samples browser with the base version of the assemblies (15.2.20152.1000) which is what I tested with. So that is why I didn't reproduce it. I'm able to see the issue now.
I logged a development issue for this to have our engineering staff examine it further. In order for you to track it I created a private support case for you which has been linked to the development issue ID. The ID is 211370 and the case number is CAS-167769-Q1Z2S4.
I guess for now you can stick with the base version if the December SR was not necessary for anything critical in your app.
I need to use the December SR since it contains several important fixes for the xamPropertyGrid. However, the since this 'UI artifact' does not make anything crash I'm happy to use the SR until a fix is available.
This issue is fixed in the WPF 20152.2100 Service Release.