Hi...
I'm trying to build out a dynamic editor using the PropertyGrid. I have had success getting the hierarchy working, but I'm running into trouble when I try to bind the data. So far, I've tried it two different ways: first, by exposing a Dictionary<string, string> (I'll leave out any type issues, and just pretend that all my non-hierarchical fields are strings, for now), and second using dynamic types. So, I set up the PropertyGrid, pretty simply in the xaml:
<telerik:RadPropertyGrid x:Name="propertyGrid1" Grid.Column="1"
SearchInNestedProperties="True"
Width="620" Height="400"
AutoGenerateBindingPaths="False"
AutoGeneratePropertyDefinitions="False"
NestedPropertiesVisibility="{Binding IsChecked, ElementName=CheckBox1, Converter={StaticResource converter}}"
DescriptionPanelVisibility="Collapsed"/>
Then, so that I can play with things, I'm adding PropertyDefinitions to propertyGrid1.PropertyDefinitions when a button is clicked. I created a simple class with a dictionary of strings in it called MyEntity:
public class MyEntity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void AddString(string key, string value)
{
_stringVal.Add(key, value);
}
private Dictionary<string, string> _stringVal = new Dictionary<string, string>();
public Dictionary<string, string> StringVal
{
get
{
return _stringVal;
}
set
{
if (value != _stringVal)
{
_stringVal = value;
NotifyPropertyChanged("StringVal");
}
}
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
Here's the class that corresponds to the xaml above:
public partial class Example : UserControl, INotifyPropertyChanged
{
private MyEntity _ent = new MyEntity();
public Example()
{
InitializeComponent();
_ent.AddString("Item0", "000");
_ent.AddString("Item1", "111");
_ent.AddString("Item2", "222");
DataContext = _ent;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var topDef = new PropertyDefinition()
{
DisplayName = "Top Item " + propertyGrid1.PropertyDefinitions.Count.ToString(),
Binding = new Binding("StringVal[Item0]")
{
Path = new PropertyPath("StringVal[Item0]")
},
OrderIndex = propertyGrid1.PropertyDefinitions.Count,
IsReadOnly = false
};
propertyGrid1.PropertyDefinitions.Add(topDef);
}
}
I can't get the data to bind though. The PropertyGrid comes up, with no errors, but the properties seem to be considered null. I can get bindings to work with paths like "StringVal[Item0]" using other controls, but not PropertyDefinitions.
I've also tried this using a dynamic type (i.e. derived from DynamicObject), instead of the MyEntity class that contains the dictionary, and gotten the same result. I prefer that approach, but thought that showing it this way would be simpler to understand.
Am I way off base here, or just missing something simple?
Thanks,
-Ari
I'm trying to build out a dynamic editor using the PropertyGrid. I have had success getting the hierarchy working, but I'm running into trouble when I try to bind the data. So far, I've tried it two different ways: first, by exposing a Dictionary<string, string> (I'll leave out any type issues, and just pretend that all my non-hierarchical fields are strings, for now), and second using dynamic types. So, I set up the PropertyGrid, pretty simply in the xaml:
<telerik:RadPropertyGrid x:Name="propertyGrid1" Grid.Column="1"
SearchInNestedProperties="True"
Width="620" Height="400"
AutoGenerateBindingPaths="False"
AutoGeneratePropertyDefinitions="False"
NestedPropertiesVisibility="{Binding IsChecked, ElementName=CheckBox1, Converter={StaticResource converter}}"
DescriptionPanelVisibility="Collapsed"/>
Then, so that I can play with things, I'm adding PropertyDefinitions to propertyGrid1.PropertyDefinitions when a button is clicked. I created a simple class with a dictionary of strings in it called MyEntity:
public class MyEntity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void AddString(string key, string value)
{
_stringVal.Add(key, value);
}
private Dictionary<string, string> _stringVal = new Dictionary<string, string>();
public Dictionary<string, string> StringVal
{
get
{
return _stringVal;
}
set
{
if (value != _stringVal)
{
_stringVal = value;
NotifyPropertyChanged("StringVal");
}
}
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
Here's the class that corresponds to the xaml above:
public partial class Example : UserControl, INotifyPropertyChanged
{
private MyEntity _ent = new MyEntity();
public Example()
{
InitializeComponent();
_ent.AddString("Item0", "000");
_ent.AddString("Item1", "111");
_ent.AddString("Item2", "222");
DataContext = _ent;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var topDef = new PropertyDefinition()
{
DisplayName = "Top Item " + propertyGrid1.PropertyDefinitions.Count.ToString(),
Binding = new Binding("StringVal[Item0]")
{
Path = new PropertyPath("StringVal[Item0]")
},
OrderIndex = propertyGrid1.PropertyDefinitions.Count,
IsReadOnly = false
};
propertyGrid1.PropertyDefinitions.Add(topDef);
}
}
I can't get the data to bind though. The PropertyGrid comes up, with no errors, but the properties seem to be considered null. I can get bindings to work with paths like "StringVal[Item0]" using other controls, but not PropertyDefinitions.
I've also tried this using a dynamic type (i.e. derived from DynamicObject), instead of the MyEntity class that contains the dictionary, and gotten the same result. I prefer that approach, but thought that showing it this way would be simpler to understand.
Am I way off base here, or just missing something simple?
Thanks,
-Ari