Column IsVisible does not bind with late DataContext binding

3 posts, 0 answers
  1. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 15 May 2014 Link to this post

    I've run into a problem where if the DataContext is set too late in the initialization/rendering process, bindings on the IsVisible property of columns are never established. Below is an example showing the problem.

    If you establish the DataContext in the constructor, it works as expected; the Number column is not shown, and you can toggle its display using the checkbox.

    However, if you comment out that line, run the app, and use the button to establish the DataContext after the grid has rendered, the Number column is shown even though ShowNumber is false, and the binding does not respond to changes in ShowNumber (again using the checkbox).

    I found a work-around by re-establishing the binding after setting the DataContext, but this is ugly and much less maintainable.

    Is there a way I can force it to update the column bindings when the DataContext is set, rather than re-creating the binding for every column on which I need to control visibility?


    <Window x:Class="DelayedBinding.MainWindow"
            Title="MainWindow" Height="350" Width="525">
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            <telerik:RadGridView Grid.Row="0" x:Name="radGridView" AutoGenerateColumns="False" ItemsSource="{Binding Path=Items}"
                                     IsFilteringAllowed="False" ShowGroupPanel="False" CanUserSortColumns="False">
                    <telerik:GridViewDataColumn Header="Name" Width="65"
                                                DataMemberBinding="{Binding Path=Name}"/>
                    <telerik:GridViewDataColumn IsVisible="{Binding Path=ShowNumber}" Header="Number" Width="65" x:Name="NumberColumn"
                                                DataMemberBinding="{Binding Path=Number}"/>
                    <telerik:GridViewDataColumn Header="Group" Width="65"
                                                DataMemberBinding="{Binding Path=Group}"/>
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <Button Click="ConnectVM" Content="ConnectVM" Margin="5"/>
                <CheckBox IsChecked="{Binding Path=ShowNumber}" Content="Show Number" VerticalAlignment="Center"/>


    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    namespace DelayedBinding
        public class Item
            public string Name { get; set; }
            public int Number { get; set; }
            public string Group { get; set; }
        public partial class MainWindow : Window, INotifyPropertyChanged
            public event PropertyChangedEventHandler PropertyChanged;
            public ObservableCollection<Item> Items { get; private set; }
            private bool _ShowNumber = false;
            public bool ShowNumber
                get { return _ShowNumber; }
                    _ShowNumber = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ShowNumber"));
            public MainWindow()
                Items = new ObservableCollection<Item>();
                for (int i = 0; i < 5; i++)
                    Items.Add(new Item() { Name = "Object " + i, Number = i, Group = (i % 2 == 1 ? "Odd" : "Even") });
                // If we connect the DataContext here, everything works fine!
                DataContext = this;
            private void ConnectVM(object sender, RoutedEventArgs e)
                // If we wait to connect it here, the ShowNumber binding isn't established!
                DataContext = this;
                // This is the work-around, by re-establishing the binding, the grid again works correctly.
                //NumberColumn.SetBinding(Telerik.Windows.Controls.GridViewColumn.IsVisibleProperty, "ShowNumber");
  2. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 15 May 2014 in reply to Louis Link to this post

    Very sorry, I just realized I posted this to the wrong forum. This is about RadGridView, not RadChartView. Could it be moved to the appropriate forum? I don't see any way for me to remove or move it myself.

  3. DevCraft R3 2016 release webinar banner
  4. Boris
    Boris avatar
    276 posts

    Posted 20 May 2014 Link to this post

    Hello Louis,

    We tested your application and there really is an issue. When we debugged the solution, we found that the DataContext gets updated, but when we tried to force update on the binding target, an error that DataContext is null occurs, which is clearly an issue with the WPF data binding mechanism. A possible cause for this behavior might be that columns of the GridView are not actually in the visual tree. 

    As an alternative to this, you can either use your workaround, or set an explicit Source to the binding, so that it does not depend on the DataContext of the column.

    Boris Penev
    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