Hi,
I am trying to use the Propertygrid (Plus version) to build a dynamic properties list.
I have been looking at the Custom Properties List examples but I am struggling to populate a combobox
with a list of string values built at runtime. I can't use an enum as I don't know the available values until a database is read.
I'm using MVVM and have bound my view to a view model.
I have created a new test class that contains a List of available strings and the currently selected string.
I am binding the test class MyData to the propertygrid properties source as in the example
```
public class ObjectWithNames
{
public ObservableCollection<string> Names { get; set; }
public string ObjectName { get; set; }
public override string ToString()
{
return ObjectName;
}
}
public class MyData
{
public MyData(string name, object value, string category)
{
this.MyName = name;
this.MyValue = value;
this.Category = category;
}
public MyData(string name, object value)
{
this.MyName = name;
this.MyValue = value;
this.Category = "Misc";
}
public string Category { get; set; }
public string MyName { get; set; }
public object MyValue { get; set; }
}
```
In My View Model I have a list of MyData objects
```
public ObservableCollection<object> SelectedLru
{
get { return _properties; }
}
private void BuildDummyProperties()
{
...
_properties.Add(new MyData("TestNames", (ObjectWithNames)new ObjectWithNames() { ObjectName = "Option 1", Names = new ObservableCollection<string>() { "Option 1", "Option 2" } }));
...
}
```
My View binds the properties source to the list of mydata objects.
I then set the xctk:EditorComboBoxDefinition TargetProperties to my test class ObjectWithNames.
```
<xctk:PropertyGrid x:Name="propertyGrid" Grid.Row="2"
PropertiesSource="{Binding SelectedLru}"
PropertyNameBinding="{Binding MyName}"
PropertyValueBinding="{Binding MyValue}">
<xctk:PropertyGrid.CategoryGroupDescription>
<PropertyGroupDescription PropertyName="Category"/>
</xctk:PropertyGrid.CategoryGroupDescription>
<xctk:PropertyGrid.Resources>
<local:MyConverter x:Key="myConverter"/>
</xctk:PropertyGrid.Resources>
<xctk:PropertyGrid.EditorDefinitions>
<xctk:EditorComboBoxDefinition TargetProperties="{x:Type local:ObjectWithNames}"
ItemsSource="{Binding MyValue.Names}
SelectedItemBinding="{Binding MyValue, Converter={StaticResource myConverter}" />
</xctk:PropertyGrid.EditorDefinitions>
</xctk:PropertyGrid>
```
This all works when the combobox items source is a static resource as in the example
```
<x:Array x:Key="comboSource1" Type="s:String" >
<s:String>String Value 1</s:String>
<s:String>String Value 2</s:String>
<s:String>String Value 3</s:String>
</x:Array>
```
However I am struggling to work out how to set the items source to the collection of strings in the ObjectWithNames class. I have tried using a Converter to see what is going on but that never seems to be called.
```
<xctk:EditorComboBoxDefinition TargetProperties="{x:Type local:ObjectWithNames}"
ItemsSource="{Binding MyValue, Converter={StaticResource myValuesConverter}"
SelectedItemBinding="{Binding MyValue, Converter={StaticResource myConverter}"
```
Any ideas would be appreciated. Is there an easier way to build the combo box items dynamically or at runtime?
Cheers
Jonathan
Comments: ** Comment from web user: emartin **
I am trying to use the Propertygrid (Plus version) to build a dynamic properties list.
I have been looking at the Custom Properties List examples but I am struggling to populate a combobox
with a list of string values built at runtime. I can't use an enum as I don't know the available values until a database is read.
I'm using MVVM and have bound my view to a view model.
I have created a new test class that contains a List of available strings and the currently selected string.
I am binding the test class MyData to the propertygrid properties source as in the example
```
public class ObjectWithNames
{
public ObservableCollection<string> Names { get; set; }
public string ObjectName { get; set; }
public override string ToString()
{
return ObjectName;
}
}
public class MyData
{
public MyData(string name, object value, string category)
{
this.MyName = name;
this.MyValue = value;
this.Category = category;
}
public MyData(string name, object value)
{
this.MyName = name;
this.MyValue = value;
this.Category = "Misc";
}
public string Category { get; set; }
public string MyName { get; set; }
public object MyValue { get; set; }
}
```
In My View Model I have a list of MyData objects
```
public ObservableCollection<object> SelectedLru
{
get { return _properties; }
}
private void BuildDummyProperties()
{
...
_properties.Add(new MyData("TestNames", (ObjectWithNames)new ObjectWithNames() { ObjectName = "Option 1", Names = new ObservableCollection<string>() { "Option 1", "Option 2" } }));
...
}
```
My View binds the properties source to the list of mydata objects.
I then set the xctk:EditorComboBoxDefinition TargetProperties to my test class ObjectWithNames.
```
<xctk:PropertyGrid x:Name="propertyGrid" Grid.Row="2"
PropertiesSource="{Binding SelectedLru}"
PropertyNameBinding="{Binding MyName}"
PropertyValueBinding="{Binding MyValue}">
<xctk:PropertyGrid.CategoryGroupDescription>
<PropertyGroupDescription PropertyName="Category"/>
</xctk:PropertyGrid.CategoryGroupDescription>
<xctk:PropertyGrid.Resources>
<local:MyConverter x:Key="myConverter"/>
</xctk:PropertyGrid.Resources>
<xctk:PropertyGrid.EditorDefinitions>
<xctk:EditorComboBoxDefinition TargetProperties="{x:Type local:ObjectWithNames}"
ItemsSource="{Binding MyValue.Names}
SelectedItemBinding="{Binding MyValue, Converter={StaticResource myConverter}" />
</xctk:PropertyGrid.EditorDefinitions>
</xctk:PropertyGrid>
```
This all works when the combobox items source is a static resource as in the example
```
<x:Array x:Key="comboSource1" Type="s:String" >
<s:String>String Value 1</s:String>
<s:String>String Value 2</s:String>
<s:String>String Value 3</s:String>
</x:Array>
```
However I am struggling to work out how to set the items source to the collection of strings in the ObjectWithNames class. I have tried using a Converter to see what is going on but that never seems to be called.
```
<xctk:EditorComboBoxDefinition TargetProperties="{x:Type local:ObjectWithNames}"
ItemsSource="{Binding MyValue, Converter={StaticResource myValuesConverter}"
SelectedItemBinding="{Binding MyValue, Converter={StaticResource myConverter}"
```
Any ideas would be appreciated. Is there an easier way to build the combo box items dynamically or at runtime?
Cheers
Jonathan
Comments: ** Comment from web user: emartin **
I think the support should have fowarded my answer by now.