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

Problem with the DataContext of Row/Cell

3 Answers 298 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lukas
Top achievements
Rank 1
Lukas asked on 26 Nov 2012, 08:39 AM
As I found out now, the DataContext of each cell is the same as its row. We got the following scenario:

We get some configuration data from an external source, which defines, how the Grid should look like. In this configuration data is defined what columns are gonna be added. This is easily done via code behind.

But now we also have a custom class for each possibly generated cell (CustomField). To show the data of this custom field, we use a custom control, which is able to handle, if any changes were made, the default value is set and so on. There is no way to change this! We thought that would easily be done, if we just put our custom control into the Cell(-Edit)Template. But as it turned out, it didn't work as expected.

The problem is the following. I briefly explain the data structure in our view model:

The property, which is the ItemsSource of our RadGridView is a collection of custom rows. Each custom row holds a collection of custom fields. But in our DataTemplate (CellTemplate), we would need to bind on the values of the custom fields like this: {Binding CustomFields[0].Value}, ... since the DataContext of a cell is the same as its rows DataContext.

We would need the data contexts as follows.
RadGridView => ICollection<DataSet> (DataSet is a collection of fields)
RadGridView.Row => ICollection<CustomField>
RadGridView.Row.Cell => CustomField

I successfully was able to change the DataContext of each cell in the CellLoaded event:

CustomGrid.CellLoaded += (sender, args) =>
    {
        if (args.Cell.GetType() == typeof (GridViewHeaderCell)) return;
        args.Cell.DataContext =
            ((CustomRow)args.Cell.DataContext).CustomFields.First(
                cf => cf.Definition.Name == args.Cell.Column.UniqueName);
    };

My problem just is, that I cannot know, when the data context is changed again. I think one point, where it changes is, as soon as I change a cells mode to EditMode. From then on the bindings fail again. Is there any way to achieve this? I'd greatly appreciate any help. Thanks!

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 26 Nov 2012, 04:37 PM
Hi Lukas ,  

I would definitely not recommend to change the DataContext of the cell. Such approach would be extremely unreliable as RadGridView will always try to reset it .

The alternative would be to use bindings with a path pointing to the required property/sub property.

The item being a DataContext for the row for any DataGrids and ItemsControls from Microsoft or 3d party vendors  is an industry standard.

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Lukas
Top achievements
Rank 1
answered on 27 Nov 2012, 10:44 AM
And how should this work? I would need to put the name of the Subproperty into the data template. And that would be "CustomFields[0].StringValue", "CustomFields[1].StringValue", etc. Whereas the data template is just defined once! I don't see any way of how achieving this properly. Is there maybe a way with dynamics?
0
Accepted
Pavel Pavlov
Telerik team
answered on 27 Nov 2012, 12:24 PM
Hello Lukas,

Indeed when defining a DataTemplate , hardcoding the binding path is required.
A way to deal with the problem would be to build the data template from string( xaml)  in code e.g. using XAML reader, building the data template in the AutogeneratingColumn event  and replacing the "path" part in a common template for  each specific column.

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Lukas
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Lukas
Top achievements
Rank 1
Share this question
or