Hello,
thank you for your answer.
I use this thread because the example of Matt Gates is very similar to my example.
Here is my simple example:
[TypeConverter(typeof(MyDesignTypeConverter))]
public class MyDesign
{
public string DesignStr;
public int DesignId;
}
public class MyDesignTypeConverter : TypeConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
var userData = context.Instance as UserData;
List<string> list = new List<string>();
if (userData != null)
{
foreach (MyDesign myDesign in userData.DesignList)
{
list.Add(myDesign.DesignStr);
}
//
}
//
return new StandardValuesCollection(list);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var userData = context.Instance as UserData;
if (userData != null && value != null)
{
object obj = userData.DesignList.FirstOrDefault(a => System.String.CompareOrdinal(a.DesignStr, value.ToString()) == 0);
return obj;
}
//
return base.ConvertFrom(context, culture, value);
}
}
//View Model
public class UserData : ViewModelBase
{
[Browsable(false)]
public IList<MyDesign> DesignList
{ get; set; }
private MyDesign _selectedMyDesign;
[LocalizedCategoryAttributes("MasterCat_MainSettings\\Cat_Appearance")]
[LocalizedDisplayNameAttributes]
[LocalizedDescriptionAttributes]
[SortOrder(1)]
[TypeConverter(typeof(MyDesignTypeConverter))]
public MyDesign SelectedMyDesign
{
get { return _selectedMyDesign; }
set
{
if (Equals(value, _selectedMyDesign)) return;
_selectedMyDesign = value;
//
OnPropertyChanged();
}
}
public UserData (MyObject parent)
: base(parent, true)
{
//
DesignList= new List<MyDesign>();
DesignList.Add(new MyDesign(){DesignId = 1, DesignStr = "Design1"});
DesignList.Add(new MyDesign() { DesignId = 2, DesignStr = "Design2" });
DesignList.Add(new MyDesign() { DesignId = 3, DesignStr = "Design3" });
SelectedMyDesign=DesignList.Add[0];
}
}
XAML:
.
.
.
<propgrid:PropertyGrid.PropertyEditors>
<propgrid:PropertyEditor ValueTemplateKey="{x:Static propgrid:BuiltinEditors.DynamicStringValueTemplateKey}" />
</propgrid:PropertyGrid.PropertyEditors>
.
.
.
[Modified 9 years ago]