I'm looking for a way to bind a List of key / value objects to the propertygrid. Consider the following snippets of code:
public
void
PopulatePropertyGrid()
{
List<MyKeyValueClass> propertyList =
new
List<MyKeyValueClass>();
propertyList.Add(
new
MyKeyValueClass(){ Key=
"property1"
, Value=
"value1"
});
propertyList.Add(
new
MyKeyValueClass(){ Key=
"property2"
, Value=
"value2"
});
propertyList.Add(
new
MyKeyValueClass(){ Key=
"property3"
, Value=
"value3"
});
//How do I bind this List to a RadPropertyGrid, so I get 3 properties with filled in values?
}
public
class
MyKeyValueClass
{
public
string
Key {
get
;
set
;}
public
string
Value {
get
;
set
;}
}
I tried something like this:
XAML:
<
telerik:RadPropertyGrid
x:Name
=
"radPropertyGrid"
AutoGeneratePropertyDefinitions
=
"false"
/>
.cs:
radPropertyGrid.PropertyDefinitions.Clear();
foreach
(MyKeyValueClass mkvc
in
propertyList)
{
radPropertyGrid.PropertyDefinitions.Add(
new
PropertyDefinition()
{
DisplayName = mkvc.Key,
Binding =
new
Binding(
"Value"
)
});
}
radPropertyGrid.Item = propertyList;
This sets the displaynames accordingly, but won't show / bind the values
Is this possible?
Stan
-Edit: code blocks formatted
15 Answers, 1 is accepted
Did you found how to solve your problem? I have almost the same in my project.
I have tested the code snippets with our current official release - Q1 2014 SP and it seems that it works correctly. Can you try testing your application with that version ? Do you have any problems with it ?
Regards,
Maya
Telerik
Dear Admin,
I am still facing this problem. I am looking for binding list of key value to the property grid.Please check the code snippet
//Expectation SET
public class ExpectationSet
{
public string Name { get; set; }
public List<
ExpectationProperty
> ExpectationProperties { get; set; }
public static List<
ExpectationSet
> AvailableSets
{
get
{
return new[]{
new ExpectationSet
{
Name ="se1",
ExpectationProperties =new[]
{
new ExpectationProperty{Name="se1.x",Value="se1x"},
new ExpectationProperty{Name="se1.z",Value="se1z"}
}.ToList()
} ,
new ExpectationSet
{
Name ="se2",
ExpectationProperties =new[]
{
new ExpectationProperty{Name="se2.x",Value="se2x"},
new ExpectationProperty{Name="se2.z",Value="se2z"},
new ExpectationProperty{Name="se2.y",Value="se2y"}
}.ToList()
},
new ExpectationSet
{
Name ="se3",
ExpectationProperties =new[]
{
new ExpectationProperty{Name="se3.p",Value="se3p"},
new ExpectationProperty{Name="se3.q",Value="se3q"},
new ExpectationProperty{Name="se3.r",Value="se3r"}
}.ToList()
}
}.ToList();
}
}
}
//ExpectationSet Properties
public class ExpectationProperty
{
public string Name { get; set; }
public string Value { get; set; }
}
// Code snippet to bind propertygrid
foreach (ExpectationProperty mkvc in ExpectationSet.AvailableSets[i].ExpectationProperties)
{
String value = mkvc.Value;
PropertyGrid1.PropertyDefinitions.Add(new PropertyDefinition()
{Binding = new System.Windows.Data.Binding("Value"),
DisplayName = mkvc.Name
});
PropertyGrid1.Item = mkvc;
}
//xaml File
<
telerik:RadPropertyGrid
x:Name
=
"PropertyGrid1"
Grid.Row
=
"2"
Grid.RowSpan
=
"2"
AutoGeneratePropertyDefinitions
=
"false"
/>
Could you clarify what the expected behavior is?
Regards,
Maya
Telerik
Dear Admin,
Thanks for the reply. Please check the sceenshot for the correct behaviour.
Thanks
DataContext of the each property definition is the object property grid is bound to. So, if you want to bind to a property of the item belonging to a property definition, you need to specify the source. For example:
var mkvc = ExpectationSet.AvailableSets[1].ExpectationProperties;
foreach (ExpectationProperty item in mkvc)
{
String value = item.Value;
PropertyGrid1.PropertyDefinitions.Add(new PropertyDefinition()
{
Binding = new System.Windows.Data.Binding("Value") {
Source = item
},
DisplayName = item.Name
});
}
PropertyGrid1.Item = mkvc;
Regards,
Maya
Telerik
Dear Maya,
Thanks for the prompt support. I have specified the source now in propertyand it works as expected.
Regards,
Payal
I am trying to show list of key/value to radpropertygrid. Consider the following snippet of :
foreach (var propValue in instance)
{
if(propValue.StringValue != null)
{
string value = propValue.StringValue;
rad_property_grid_1.PropertyDefinitions.Add(new PropertyDefinition()
{
Binding = new System.Windows.Data.Binding("value") ,
DisplayName = propValue.Property.DisplayLabel //Property.DisplayLabel
});
}
}
i am able to show the keys but in case for values it is not working
i am looking to bind key-values to my radpropetrygrid. Consider following code:
foreach (var data in properties)
{
if (data.Value != null)
{
var value = data.Value;
radPropertGrid1.PropertyDefinitions.Add(new PropertyDefinition()
{
Binding = new System.Windows.Data.Binding(value),
DisplayName = data.key
});
}
}
While I am not aware of the exact setup on your side, I have prepared a sample project which demonstrates how you can bind key-values to RadPropertyGrid based on the earlier discussion in the thread. May I ask you to check it out and see how it differs from the setup at your end?
Should you need any further assistance, may I ask you to open a new support ticket/forum thread and provide some additional information about your exact setup and potentially a sample project demonstrating the difficulties that you have encountered?
Regards,
Vladimir Stoyanov
Progress Telerik
Hi Admin,
I ran the application but it is showing same value for all properties. I have attached the output file, please refer for the same.
Thanks,
Shweta
Thank you for the attached picture.
You are correct about that. I missed to specify the Source property of the Binding. Here is the updated binding:
foreach
(MyKeyValueClass mkvc
in
propertyList)
{
radPropertyGrid.PropertyDefinitions.Add(
new
PropertyDefinition()
{
DisplayName = mkvc.Key,
Binding =
new
Binding(
"Value"
) { Source = mkvc }
});
}
I am also adding the modified project for your reference.
Please let me know if you have any further questions.
Regards,
Vladimir Stoyanov
Progress Telerik
Hi Admin,
Thank you. It works as expected.
Regards,
Shweta
Hello,
I am doing the same but I also want to display the nested properties coming from the Value in MyKeyValueClass. How can I display nested properties?
Regards,
Monique
Hello Monique,
I've updated the project Vladimir provided to demonstrate a possible approach for binding nested properties which are a part of the MyKeyValueClass. I've done this by adding new PropertyDefinitions to the parent definition's NestedProperties collection.
foreach (MyKeyValueClass mkvc in propertyList)
{
var propertyDefinition = new PropertyDefinition()
{
DisplayName = mkvc.Key,
Binding = new Binding("NestedKeyValuePair") { Source = mkvc },
};
propertyDefinition.NestedProperties.Add(new PropertyDefinition()
{
DisplayName = mkvc.NestedKeyValuePair.Key,
Binding = new Binding("Value") { Source = mkvc },
});
radPropertyGrid.PropertyDefinitions.Add(propertyDefinition);
}
Regards,
Dilyan Traykov
Progress Telerik