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

Adding properties in runtime.

5 Answers 428 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Axel Lekander
Top achievements
Rank 1
Axel Lekander asked on 03 Aug 2011, 05:00 PM
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

5 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 04 Aug 2011, 11:22 AM
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 >>
0
Dolf
Top achievements
Rank 1
answered on 17 Aug 2011, 03:12 PM
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
0
Ivan Ivanov
Telerik team
answered on 19 Aug 2011, 10:39 AM
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 >>
0
Mauricio
Top achievements
Rank 1
answered on 29 Apr 2012, 09:13 AM

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

0
Mauricio
Top achievements
Rank 1
answered on 29 Apr 2012, 07:44 PM
This is exactly what has solved my problem, in case someone else is looking on how to.
Dynamic Code Generation

Thanks.
Tags
PropertyGrid
Asked by
Axel Lekander
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Dolf
Top achievements
Rank 1
Mauricio
Top achievements
Rank 1
Share this question
or