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

DataContext of GridViewColumns not inherited

10 Answers 458 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 07 Aug 2014, 10:21 AM
Hello!
With the Q2 2014 version, there seems to be some breaking change in the Column handling of the RadGridView:

With the Q1 version, the DataContext of the columns was inherited, however, with the newer Q2 versions (also with the recent one), this is not the case for _one_ of our GridViews. All (or at least most) others are still working fine, but I do not see the difference to the grid where it fails.

Do you have any idea what could be the reason that the datacontext of the columns is not inherited - or how I could debug it? The columns are - obviously - not in the visual tree - so with Snoop I only see that the datacontext is not set, but I don't see the reason why...

Alex

10 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 11 Aug 2014, 12:11 PM
Hello Alex,

I checked the release notes between versions Q1 2014 and Q2 2014 and I did not notice any such change.

May I ask you why do you need this DataContext? What goal would you like to achieve, so that I can try suggesting you another way rather than relying on the DataContext? 

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Alexander
Top achievements
Rank 1
answered on 18 Aug 2014, 06:25 AM
There are a large number of dependency properties on the columns. If I want to bind for example the ItemsSource or the visibility of a column, I need the DataContext to bind these properties.
However, as the columns are not in the visual tree, I don't know how the DataContext is inherited or why this is not the case for one GridView.

Alex
0
Dimitrina
Telerik team
answered on 18 Aug 2014, 08:06 AM
Hello Alex,

I was able to reproduce the issue locally and I am going to further investigate it.

Meanwhile, I can suggest you to explicitly specify the Source for the binding through a StaticResource ((as explained here). 

For example:

IsVisible="{Binding IsVisible, Source={StaticResource MyDataContext}}"


Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Alexander
Top achievements
Rank 1
answered on 19 Aug 2014, 08:24 AM
Thank you for your efforts. Do you already have any idea why it fails only on some GridViews, but not all?
Is there perhaps a global fix for the whole GridView so I don't have to change all bindings?

Alex
0
Dimitrina
Telerik team
answered on 19 Aug 2014, 08:48 AM
Hi Alex,

It does actually work in most the cases. It seems it falls in some scenarios only.
I tried reproducing it in a very sample demo without a success.
You can find attached a demo project to test locally. Would you please check it and the try modifying it to replicate the exact case you have?

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Alexander
Top achievements
Rank 1
answered on 19 Aug 2014, 09:49 AM
I think I know what the problem is:

if you change the DataContext of the RadGridView after it has been loaded (or after the columns have been created), the change is not reflected in the Columns. The DataContext in the columns is still the old value. In the Q1 version, the change was propagated to the columns as expected. E.g., if I clear it on the RadGridView, it is also empty on the Columns.

In the case where it does not work, we used the DataGrid in some Layout container which inserts the previously created content at some location in the layout control. As the DataContext is updated on insertion, it does not get propagated to the column.

Alex
0
Dimitrina
Telerik team
answered on 19 Aug 2014, 04:44 PM
Hello Alex,

Thank you for those notes, I was now able to reproduce it in my demo project.
I updated the bug report with this addition information.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Alexander
Top achievements
Rank 1
answered on 21 Aug 2014, 03:10 PM
Just in case anybody has the same problem:
As I now had to upgrade to the newest version (in the old version, the DragDropManager was not working as expected; now it does)
I use as a workaround (until the bug is fixed) the following attached property (simply use GridExtensions.FixColumnContext="True" on the GridView):

public static class GridExtensions
{
    public static bool GetFixColumnContext(RadGridView element)
    {
        return (bool)element.GetValue(FixColumnContextProperty);
    }
  
    public static void SetFixColumnContext(RadGridView element, bool value)
    {
        element.SetValue(FixColumnContextProperty, value);
    }
  
    /// <summary>
    /// Bugfix for RadGridView: update DataContext for columns.
    /// </summary>
    public static readonly DependencyProperty FixColumnContextProperty = DependencyProperty.RegisterAttached("FixColumnContext", typeof(bool), typeof(GridExtensions), new PropertyMetadata(false, OnFixColumnContextChanged));
  
    private static void OnFixColumnContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var grid = (RadGridView)d;
        if (true.Equals(e.NewValue))
        {
            grid.DataContextChanged += grid_DataContextChanged;
        }
        else
        {
            grid.DataContextChanged -= grid_DataContextChanged;
        }
    }
  
    static void grid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var grid = (RadGridView)sender;
  
        foreach (var column in grid.Columns)
        {
            column.DataContext = grid.DataContext;
        }
    }
}

Alex
0
Dimitrina
Telerik team
answered on 22 Aug 2014, 09:36 AM
Hello Alex,

Thank you for sharing your workaround with the community.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Dimitrina
Telerik team
answered on 01 Sep 2014, 07:57 AM
Hi,

I am writing with an update that you can now check the progress of the issue in our Feedback Portal: GridViewColumn.DataContext is not inheritedYou can also follow the item in the feedback portal. That way you will get a notification every time its status gets changed.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Alexander
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Alexander
Top achievements
Rank 1
Share this question
or