DataContext of GridViewColumns not inherited

11 posts, 0 answers
  1. Alexander
    Alexander avatar
    78 posts
    Member since:
    Apr 2013

    Posted 07 Aug 2014 Link to this post

    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
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 11 Aug 2014 Link to this post

    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.
     
  3. Alexander
    Alexander avatar
    78 posts
    Member since:
    Apr 2013

    Posted 18 Aug 2014 in reply to Dimitrina Link to this post

    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
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 18 Aug 2014 Link to this post

    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.
     
  5. Alexander
    Alexander avatar
    78 posts
    Member since:
    Apr 2013

    Posted 19 Aug 2014 in reply to Dimitrina Link to this post

    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
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Aug 2014 Link to this post

    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.
     
  7. Alexander
    Alexander avatar
    78 posts
    Member since:
    Apr 2013

    Posted 19 Aug 2014 in reply to Dimitrina Link to this post

    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
  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Aug 2014 Link to this post

    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.
     
  9. Alexander
    Alexander avatar
    78 posts
    Member since:
    Apr 2013

    Posted 21 Aug 2014 in reply to Dimitrina Link to this post

    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
  10. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Aug 2014 Link to this post

    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.
     
  11. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 01 Sep 2014 Link to this post

    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.
     
Back to Top