This is a migrated thread and some comments may be shown as answers.

Dynamic objects in RadPropertyGrid

15 Answers 786 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
stan0611
Top achievements
Rank 1
stan0611 asked on 24 Sep 2013, 03:15 PM
Hi,

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

Sort by
0
Ilya
Top achievements
Rank 1
answered on 14 May 2014, 07:47 AM
Hi!

Did you found how to solve your problem? I have almost the same in my project.
0
Maya
Telerik team
answered on 19 May 2014, 07:17 AM
Hello Illya,

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
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
payal
Top achievements
Rank 1
answered on 02 May 2016, 04:37 PM

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"
                                       
                                   />

0
Maya
Telerik team
answered on 03 May 2016, 07:12 AM
Hello Payal,

Could you clarify what the expected behavior is?

Regards,
Maya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
payal
Top achievements
Rank 1
answered on 03 May 2016, 08:13 AM

Dear Admin,

 

Thanks for the reply. Please check the sceenshot for the correct behaviour.

 

Thanks

 

 

0
Maya
Telerik team
answered on 03 May 2016, 08:31 AM
Hello Payal,

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
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
payal
Top achievements
Rank 1
answered on 03 May 2016, 08:48 AM

Dear Maya,

 

Thanks for the prompt support. I have specified the source now in propertyand it works as expected.

 

Regards,

Payal

0
Shivkanya
Top achievements
Rank 1
answered on 29 May 2018, 11:55 AM

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

 

 

0
Shweta
Top achievements
Rank 1
answered on 31 May 2018, 05:24 AM
Hello Maya,

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
                                        });
                                    }
                                }
0
Vladimir Stoyanov
Telerik team
answered on 01 Jun 2018, 09:37 AM
Hello Shweta and Shivkanya,

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
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Shweta
Top achievements
Rank 1
answered on 05 Jun 2018, 06:59 AM

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 

0
Vladimir Stoyanov
Telerik team
answered on 05 Jun 2018, 12:59 PM
Hello 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
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Shweta
Top achievements
Rank 1
answered on 07 Jun 2018, 10:11 AM

Hi Admin,

Thank you. It works as expected.

 

Regards,

Shweta 

0
Monique
Top achievements
Rank 1
answered on 05 Nov 2019, 10:31 PM

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

0
Dilyan Traykov
Telerik team
answered on 08 Nov 2019, 12:04 PM

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);
            }
Please have a look and let me know if a similar approach would work at your end.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PropertyGrid
Asked by
stan0611
Top achievements
Rank 1
Answers by
Ilya
Top achievements
Rank 1
Maya
Telerik team
payal
Top achievements
Rank 1
Shivkanya
Top achievements
Rank 1
Shweta
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Shweta
Top achievements
Rank 1
Monique
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or