Hello,
First, I'm loading a PropertyGrid with dynamically generated PropertyDefintions at run-time, instead of hard coded XAML. Second, for one PropertyGrid entry, I'm trying to load a Combobox whose model includes both a value and the list of items to show in the Combobox. Unfortunately when I try to do this, I just get a blank Combobox (nothing in either the entry or the dropdown list) and an error:
System.Windows.Data Error: 40 : BindingExpression path error: 'Items' property not found on 'object' ''ObservableCollection`1' (HashCode=35225966)'. BindingExpression:Path=Items; DataItem='ObservableCollection`1' (HashCode=35225966); target element is 'RadComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
So obviously XAML does not "see" Items from the model, but I thought I had everything defined properly (see code below). So I'm missing something somewhere. Or maybe something is not structured right. I've looked at several example projects given in this forum, but nothing works or the sample does not fit what I'm trying to do.
The goal is to emulate an Enum but make it dynamic by loading the Comboxbox with dynamically generated lists, and I want both the entry and the list of items to be dynamically generated in the model. Any suggestions would be welcome!!
Thanks in advance
Here is where the property definitions are being loaded
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PropertyDefinitionCollection properties = EditorPropertyGrid.PropertyDefinitions;
ObservableCollection<
object
> testList = new ObservableCollection<
object
>();
// WORKS
TestEnumKeyValue first = new TestEnumKeyValue() { Key = "Static Enum Prop", Value = TestEnum.test3 };
testList.Add(first);
properties.Add(new PropertyDefinition()
{
DisplayName = first.Key,
Binding = new Binding("Value") { Source = first }
});
// DOES NOT WORK
ComboListKeyValue third = new ComboListKeyValue() { Key = "List Prop", Value = "A3" };
testList.Add(third);
properties.Add(new PropertyDefinition()
{
DisplayName = third.Key,
Binding = new Binding("Value") { Source = third } ,
EditorTemplate = (DataTemplate)EditorGrid.Resources["ComboBoxTemplate"]
});
EditorPropertyGrid.Item = testList;
}
Here is the model
enum TestEnum
{
test1,
test2,
test3
}
abstract class KeyValueBase
{
public string Key { get; set; }
}
class TestEnumKeyValue : KeyValueBase
{
public TestEnum Value { get; set; }
}
class ComboListKeyValue : KeyValueBase, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _value;
private List<
string
> _values;
public ComboListKeyValue()
{
_values = new List<
string
>() // list for combobox dynamically generated here
{
"A1",
"A2",
"A3",
"A4"
};
}
public string Value
{
get { return this._value; }
set
{
if (value != this._value)
{
this._value = value;
this.OnPropertyChanged("Value");
}
}
}
public List<
string
> Items
{
get
{
return _values;
}
set
{
_values = value;
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, args);
}
}
private void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
And here is the XAML
<
Grid
x:Name
=
"EditorGrid"
>
<
Grid.Resources
>
<
DataTemplate
x:Key
=
"ComboBoxTemplate"
>
<
telerik:RadComboBox
ItemsSource
=
"{Binding Items, Mode=OneWay}"
SelectedValue
=
"{Binding Value}"
/>
</
DataTemplate
>
</
Grid.Resources
>
<
telerik:RadPropertyGrid
x:Name
=
"EditorPropertyGrid"
Margin
=
"10,10,10,10"
DescriptionPanelVisibility
=
"Collapsed"
SearchBoxVisibility
=
"Collapsed"
AutoGeneratePropertyDefinitions
=
"False"
NestedPropertiesVisibility
=
"Visible"
AutoGenerateBindingPaths
=
"False"
SortAndGroupButtonsVisibility
=
"Collapsed"
/>
</
Grid
>
Thanks!