This question is locked. New answers and comments are not allowed.
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:
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!
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!