This project is read-only.

list a string[] as a DropDown Box in the WPG

Apr 20, 2010 at 7:39 AM

Hi!

Is it possible to display a string - array in a DropDownbox in the propertygrid?

 

thx,

Martin

May 12, 2010 at 7:09 AM

hi, I am also looking to implement suppport for List/Collection/ObservableCollection type properties in my property grid. Displaying a list of values is not a problem, I am confused on how to keep track of selected item(or say pass it to client).

Any ideas ?

Nov 8, 2010 at 3:02 PM

akoshi

 

You could do something like this:

<Combox ItemsSourse="{Binding Path=Value}" SelectedValue="{Binding Path=Value}"/>

However, I am unable to get a list of value to display. I want to display a List<string> as a combobox. Any ways to do this? I would even take a hack method at this point...Thanks!

Jan 24, 2011 at 3:35 PM
Edited Jan 25, 2011 at 7:44 AM

Hi!

 

I did something like this... for example i would like to show a list of persons in combobox and show current selected person... 

Here's my persons class:

 

 [DisplayName("Persons")]
        public class Persons
        {
            [Browsable(true)]
            [DisplayName("CurrentPerson")]
            [Category("Names")]
            [TypeConverter(typeof(ListTypeConverter))]
            public string CurrentPerson
            {
                get;
                set;
            }

            public Persons()
            {
                this.CurrentPerson = "John";
            }
        }

 

ListTypeConverter is like this:
        public class ListTypeConverter : TypeConverter
        {
            ...
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {

                return value as string;
            }

            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                var tmp = new List<string>() { "Tom", "John", "Din", "Mike" };
                var en = tmp.GetEnumerator();
                do
                {
                    if (!en.MoveNext())
                        break;
                } while (!en.Current.Equals(value as string));

                return tmp;
            }

            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return destinationType.Equals(typeof(List<string>));
            }
        }

Next i had to make some changes in Property.cs such as...

 

public Type PropertyType
        {
            get
            {
                if (this.Value is IList)
                    return typeof(IList);
                else
                    return _property.PropertyType;
            }
        }

 

and 

 

public object Value
        {
            get 
            {
                this._currentValue = _property.GetValue(_instance);
                var converter = _property.Converter;
                if(converter.CanConvertTo(typeof(List<string>)))
                {
                    return converter.ConvertTo(_property.GetValue(_instance),typeof(List<string>));
                }
                return _property.GetValue(_instance);
            
            }
            set
            {
                object currentValue = _property.GetValue(_instance);
                if (value != null && value.Equals(currentValue))
                {
                    return;
                }
                Type propertyType = _property.PropertyType;
                if (propertyType == typeof(object) ||
                    value == null && propertyType.IsClass ||
                    value != null && propertyType.IsAssignableFrom(value.GetType()))
                {
                    _property.SetValue(_instance, value);
                }
                else
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(_property.PropertyType);
                    try
                    {
                        object convertedValue = converter.ConvertFrom(value);
                        _property.SetValue(_instance, convertedValue);
                    }
                    catch (Exception)
                    { }
                }
            }
        }
        private object _currentValue = null;
        public object CurrentValue 
        {
            get
            {
                return this._currentValue;
            }
            set
            {
                object currentValue = _property.GetValue(_instance);
                if (value != null && value.Equals(currentValue))
                {
                    return;
                }
                Type propertyType = _property.PropertyType;
                if (propertyType == typeof(object) ||
                    value == null && propertyType.IsClass ||
                    value != null && propertyType.IsAssignableFrom(value.GetType()))
                {
                    _property.SetValue(_instance, value);
                }
                else
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(_property.PropertyType);
                    try
                    {
                        object convertedValue = converter.ConvertFrom(value);
                        _property.SetValue(_instance, convertedValue);
                    }
                    catch (Exception)
                    { }
                }
                this._currentValue = value;
            }
        }

 

Also some changes in WPGTemplates.xaml like:

 

	<DataTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:PropertyGrid}, ResourceId={x:Type clr2:IList}}">
        <ComboBox SelectedValue="{Binding Path=CurrentValue}">
            <ComboBox.ItemsSource>
                <Binding Path="Value">
                </Binding>
            </ComboBox.ItemsSource>
        </ComboBox>
	</DataTemplate>

And after that i have a grid cell that give opportunity to choose all available persons and shows current person. Good luck and sorry for my English...

 

Aug 2, 2011 at 1:57 PM

Hi there,

Thank you for posting the code, I need a rather small clarification.

Right now My xaml code look like this.I am new to the WPG and WPF. Please tell me where to add the 'DataTemplate' in the sample below. The issue I am facing is , still I cannot get the ComboBox ; instead I can see the TextBox with text "System.ComponentModel.TypeConverter+StandardValuesCollection". Please advise.

  <Window x:Class="VisuPluginTester.PropertyGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:clr3="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
    xmlns:wpg="clr-namespace:WPG;assembly=WPG"       
    Title="PropertyGrid" Height="300" Width="300">
    <DockPanel VerticalAlignment="Stretch" >
        <wpg:PropertyGrid Name="wpg" DockPanel.Dock="Top" VerticalAlignment="Stretch" IsEnabled="True" ShowDescription="True">
        </wpg:PropertyGrid>      
    </DockPanel>
</Window>