Adding properties in runtime.

6 posts, 0 answers
  1. Axel Lekander
    Axel Lekander avatar
    1 posts
    Member since:
    Nov 2009

    Posted 03 Aug 2011 Link to this post

    Hi

    I have been using the property grid and I like it a lot, but now i have problem. Is there a way to add "properties" at runtime?

    I have been trying to get a datatable/datarow to work with the property grid with no luck.

    Any ideas?

    Axel
  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 04 Aug 2011 Link to this post

    Hello Axel Lekander,

    Since RadPropertyGrid's PropertyDefinitions property is of type PropertyDefinitionCollection, which inherits from ObservableCollection<PropertyDefinition>, the control is aware of all the collection changes you are applying at runtime. I have prepared a very simple project that illustrates this. However, if you experience any specific problems with your scenario, feel free to send us you project, in order to debug it for you. 

    Kind regards,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dolf
    Dolf avatar
    1 posts
    Member since:
    Aug 2011

    Posted 17 Aug 2011 Link to this post

    Hi Ivan

    I have a class that contains a dictionary object. How would I bind the propertygrid to display each kevalue pair as a property in the grid. the data class looks like this
    public class SubjectData
        {
            public Dictionary<string, object> Attributes { get; set; }
     
            public SubjectData()
            {
                Attributes = new Dictionary<string, object>();
     
                Attributes.Add("MyProp", "1234abc");
                Attributes.Add("MyProp2", 12345);
                Attributes.Add("MyProp3", true);
            }
        }

    And this is where I create the properties and add it to the grid
    public MainWindow()
    {
        InitializeComponent();
        radPropertyGrid.Item = new SubjectData();
     
        PropertyDefinition p = new PropertyDefinition();
        p.Binding = new Binding("Attributes");
     
        p.DisplayName = "Property 1";
     
        radPropertyGrid.PropertyDefinitions.Add(p);
    }

    I expect to see the property name and a textbox with the value as specified in the Dictionary.

    Please help
    Dolf
  5. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 19 Aug 2011 Link to this post

    Hi Dolf,

     Setting just the Binding property of the added PropertyDefinitions won't work properly here. You should define your own editor templates that have appropriate bindings set. I would advise you to bind them to the "Attributes" property and use a converter, passing the respective key as a converter parameter. 

    Best wishes,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  6. Mauricio
    Mauricio avatar
    3 posts
    Member since:
    Aug 2011

    Posted 29 Apr 2012 Link to this post

    Hi Ivan, I am struggling to get there as well. I need to populate the fields that come from a SQL query at runtime. I don't have any problem creating the XAML, as this is a trivial task. My issue is with the ViewModel (I'm using MVVM).

    These are the choices that have tested unsucessfully:
    1. Expando objects: work fine but cannot create from XML
    2. DataRow: this is the closest match since it would be straigthforward but it doesn't show initial values, however it saves them, so its 50% of the solution
    3. Dictionary: tried to follow your advise on that, but I have no clue (any snippet perhaps?)
    4. Plain XML: as before with expando test, it works with XAML, which is great but how can I map that properties with XML as datasource? again totally nuts...

    Any tip, help, link, thought, would be highly appreciated.

    Thanks!

    Mauricio

  7. Mauricio
    Mauricio avatar
    3 posts
    Member since:
    Aug 2011

    Posted 29 Apr 2012 Link to this post

    This is exactly what has solved my problem, in case someone else is looking on how to.
    Dynamic Code Generation

    Thanks.
Back to Top
UI for WPF is Visual Studio 2017 Ready