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?
Thanks,
Louis
XAML:
Code-behind:
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?
Thanks,
Louis
XAML:
<Window x:Class="DelayedBinding.MainWindow" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <telerik:RadGridView Grid.Row="0" x:Name="radGridView" AutoGenerateColumns="False" ItemsSource="{Binding Path=Items}" IsFilteringAllowed="False" ShowGroupPanel="False" CanUserSortColumns="False"> <telerik:RadGridView.Columns> <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}"/> </telerik:RadGridView.Columns> </telerik:RadGridView> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Click="ConnectVM" Content="ConnectVM" Margin="5"/> <CheckBox IsChecked="{Binding Path=ShowNumber}" Content="Show Number" VerticalAlignment="Center"/> </StackPanel> </Grid></Window>Code-behind:
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; } set { _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") }); InitializeComponent(); // 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"); } }}