Problem with the DataContext of Row/Cell

4 posts, 1 answers
  1. Lukas
    Lukas avatar
    20 posts
    Member since:
    Jul 2011

    Posted 26 Nov 2012 Link to this post

    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!
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 26 Nov 2012 Link to this post

    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.

  3. Lukas
    Lukas avatar
    20 posts
    Member since:
    Jul 2011

    Posted 27 Nov 2012 Link to this post

    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?
  4. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 27 Nov 2012 Link to this post

    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.

Back to Top