DataGridTextColumn.Visibility Binding Issue via XAML

Mar 1, 2010 at 1:09 AM

Binding to DataGridTextColumn.Visibility does not work via XAML, but does work in code behind.

Is someone able to point out if this is a known bug in DataGridTextColumn or have I not done my binding correct?

Code provided below.

Thanks in advance.


// --------------- DataGridColumnVisibilityBinding.cs ---------------

<Window x:Class="WpfApplication2.DataGridColumnVisibilityBinding" Name="This"
    Title="Window DataGrid Visibility Binding" Height="300" Width="300">
        <wpf:DataGrid Name="_datagrid" ItemsSource="{Binding ElementName=This, Path=Persons}" Margin="5" AutoGenerateColumns="False">           
                <wpf:DataGridTextColumn Header="Name" Width="Auto" Binding="{Binding Path=Name}" />
                <!-- Visibility Binding Via XAML (To use binding via XAML, comment out the next line and uncomment the line after, make sure to comment out the code behind) -->
                <wpf:DataGridTextColumn Header="Surname" Width="Auto" Binding="{Binding Path=Surname}" />
                <!--wpf:DataGridTextColumn Header="Surname" Width="Auto" Binding="{Binding Path=Surname}" Visibility="{Binding Source=This, Path=VisibilityStatus}" /-->
                <wpf:DataGridTextColumn Header="Age" Width="Auto" Binding="{Binding Path=Age}" />

        <Button Width="Auto" Margin="3" Content="Click To Change Visibility" Click="VisibilityChange_Click"/>


// --------------- DataGridColumnVisibilityBinding.xaml ---------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Windows.Controls;

namespace WpfApplication2
    public partial class DataGridColumnVisibilityBinding : Window, INotifyPropertyChanged
        private ObservableCollection<Person> _Persons = new ObservableCollection<Person>(){
            new Person(){Name="aaa", Surname="AAA", Age=30},
            new Person(){Name="bbb", Surname="BBB", Age=40},
            new Person(){Name="ccc", Surname="CCC", Age=50}
        public ObservableCollection<Person> Persons
            get { return _Persons; }

        public DataGridColumnVisibilityBinding()

            // Vibility Binding Via Code (To use the xaml binding, uncomment the next 5 lines and change the xaml file)
            Binding _binding = new Binding("VisibilityStatus");
            _binding.Source = this.This;
            _binding.Mode = BindingMode.OneWay;
            DataGridTextColumn _col = (DataGridTextColumn)this._datagrid.Columns[1];
            BindingOperations.SetBinding(_col, DataGridTextColumn.VisibilityProperty, _binding);

        private void VisibilityChange_Click(object sender, RoutedEventArgs e)
            if (this.VisibilityStatus == Visibility.Visible)
                this.VisibilityStatus = Visibility.Collapsed;
                this.VisibilityStatus = Visibility.Visible;

        private Visibility _VisibilityStatus = Visibility.Visible;
        public Visibility VisibilityStatus
            get { return _VisibilityStatus; }
                _VisibilityStatus = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("VisibilityStatus"));

        public event PropertyChangedEventHandler PropertyChanged;

    public class Person
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }


Apr 22, 2010 at 12:16 PM

Also, if you want to use two way binding (from code behind) you can extend DataGridColumn:

    public class ExtDataGridTextColumn : DataGridTextColumn, INotifyPropertyChanged
        public new Visibility Visibility
            get { return base.Visibility; }
                base.Visibility = value;
                PropertyChanged.Fire(this, new PropertyChangedEventArgs("Visibility"));

        public event PropertyChangedEventHandler PropertyChanged;
But binding in this case does not work in XAML.