Dynamic objects in RadPropertyGrid

8 posts, 0 answers
  1. stan0611
    stan0611 avatar
    33 posts
    Member since:
    Jun 2006

    Posted 24 Sep 2013 Link to this post

    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
  2. Ilya
    Ilya avatar
    3 posts
    Member since:
    May 2013

    Posted 14 May 2014 in reply to stan0611 Link to this post

    Hi!

    Did you found how to solve your problem? I have almost the same in my project.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 19 May 2014 Link to this post

    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.
     
  5. payal
    payal avatar
    8 posts
    Member since:
    May 2016

    Posted 02 May in reply to Maya Link to this post

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

  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 May Link to this post

    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.
  7. payal
    payal avatar
    8 posts
    Member since:
    May 2016

    Posted 03 May in reply to Maya Link to this post

    Dear Admin,

     

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

     

    Thanks

     

     

  8. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 May Link to this post

    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.
  9. payal
    payal avatar
    8 posts
    Member since:
    May 2016

    Posted 03 May in reply to Maya Link to this post

    Dear Maya,

     

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

     

    Regards,

    Payal

Back to Top
UI for WPF is Visual Studio 2017 Ready