This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi,
I have a simple List<T> which I am trying to bind to a combo box with no success. I've tried using the ListSelectEditor from the editorTemplates but that uses an EnumValuesConverter which never returns my collection and the only thing that I see in my combo box is 1 entry "Collections". Would you be able to provide a sample of how I would bind the following using the WPF Property Grid
List<KeyValue> k= new List<KeyValue> () k.add(new KeyValue(1,1)); k.add(new KeyValue(2,2)); want to be able to bind k, to see 1 and 2 in the dropdown.
Thanks, Justin |
|
|
I'm not sure I've understood your scenario here. Are you saying you have a property of type List<T> and you want to show that property in a drop-down instead of as an expandable list? Or you have a property of type int and you want to show that property as a drop-down with a list of suggested values (1 and 2 in your example)? I am guessing it is the latter in which case the easiest solution is probably just to implement a type converter for the property and override GetStandardValuesSupported() and GetStandardValues(). |
|
|
Thanks Ivan, I might of been a little of unclear. I do want to have a drop down with the 2 values. Below is exactly what im doing <ms:PropertyEditor PropertyName="ActivityNames" EditorTemplate="{StaticResource {x:Static ms:PropertyGrid.ListSelectEditorKey}}"/> I have a List<ActivityGotoValue > ActivityNames { get; set; }
public class ActivityGotoValue : TypeConverter { public string Value { get; set; } public ActivityGotoValue() { }
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { return base.ConvertFrom(context, culture, value); } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return base.GetStandardValues(context); } } Would this be correct or do I have to create my own EditorTemplate and not use the ListSelectEditorKey |
|
|
The TypeConverter approach would work if you had a property of type ActivityGotoValue and wanted the user to be able to select a value for that property from a list. However, from your first code fragment, you actually have a property of type List<ActivityGotoValue>. What is your intent here? Is this purely to display the values stored in the property, or do you want the user to be able to add and remove values (and/or edit existing values)? What is the meaning of the user selecting a value from the drop-down? A drop-down usually implies to a user that they are selecting one value, but your ActivityNames property is a list and therefore contains multiple values. If your intent is really that the user can edit the ActivityNames list then I think a drop-down might not be the ideal UI here, but perhaps I am still misunderstanding your intent... |
|
|
Hi Ivan,
I just want to display a list of strings to the user where they can select one, just like a simple drop down list. I Originally had ActivitiesNames as a simple List<String> but that wasnt working. |
|
|
Righty ho, so you have a property of type string and you want the user to be able to select the value from a drop-down. In that case the TypeConverter is indeed the way to go. If you are using strings then you don't need to worry about the ConvertFrom side of things (though you will if you make the property of type ActivityGotoValue instead of string): you just need GetStandardValues and GetStandardValuesSupported. internal class RevolutionaryBiscuitConverter : TypeConverter Now you need to tell the property grid that you want to use this converter for your property. If you were using a custom type like ActivityGotoValue then you could specify the type converter at the class level but of course you can't do that with string. Fortunately you can apply TypeConverterAttribute at the property level as well: private string _biscuit; (Note that the property is of type string, NOT List<string>.) Pop that in the property grid and you will see that the Biscuit property now shows a drop-down instead of a text box. Hope that makes sense now and sorry for any confusion on my part! |
|
|
Oh... and to be clear, with this in place you will NOT need to set up a PropertyEditor -- the presence of the TypeConverter and GetStandardValuesSupported is enough to clue the property grid in to use a drop-down. |
|