Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 754727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:02:50+00:00 2026-05-14T15:02:50+00:00

I want to bind datagrid view column visibility with a property of class. I

  • 0

I want to bind datagrid view column visibility with a property of class.
I am passing a collection as ItemSource to grid.

I am not able to do this. Any idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:02:50+00:00Added an answer on May 14, 2026 at 3:02 pm

    This one is a bit tricky. The problem comes from the fact that DataGrid.Columns is just a property and not part of the visual tree. This means that normal binding tools like ElementName or RelativeSource will not work. If, however, you override the Metadata for the DataGrid.DataContext property, you can get it to work properly. Example code below:

    <Window x:Class="DataGridColumnVisibilitySample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
        xmlns:l="clr-namespace:DataGridColumnVisibilitySample"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <l:VisibilityConverter x:Key="VisibilityC" />
        </Window.Resources>
        <DockPanel LastChildFill="True">
            <CheckBox DockPanel.Dock="Top" Margin="8" Content="Show Column B" IsChecked="{Binding ShowColumnB}" />
            <tk:DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" CanUserAddRows="False">
                <tk:DataGrid.Columns>
                    <tk:DataGridTextColumn Header="Column A" Binding="{Binding ColumnA}" />
                    <tk:DataGridTextColumn Header="Column B" Binding="{Binding ColumnB}"
                                           Visibility="{Binding (FrameworkElement.DataContext).ShowColumnB,
                                                                RelativeSource={x:Static RelativeSource.Self},
                                                                Converter={StaticResource VisibilityC}}" />
                    <tk:DataGridTextColumn Header="Column C" Binding="{Binding ColumnC}" />
                </tk:DataGrid.Columns>
            </tk:DataGrid>
        </DockPanel>
    </Window>
    

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq;
    using System.Windows;
    using System.Windows.Data;
    using Microsoft.Windows.Controls;
    
    namespace DataGridColumnVisibilitySample
    {
        public partial class Window1 : INotifyPropertyChanged
        {
            public Window1()
            {
                InitializeComponent();
                new DataGridContextHelper();  // Initialize Helper
                Items = Enumerable.Range(1, 3).Select(i => new MyItem {ColumnA = "A" + i, ColumnB = "B" + i, ColumnC = "C" + i}).ToList();
                DataContext = this;
            }
    
            public List<MyItem> Items { get; private set; }
    
            private bool mShowColumnB;
            public bool ShowColumnB
            {
                get { return mShowColumnB; }
                set
                {
                    if (mShowColumnB == value) return;
                    mShowColumnB = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ShowColumnB"));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public class DataGridContextHelper
        {
            static DataGridContextHelper()
            {
                FrameworkElement.DataContextProperty.OverrideMetadata(typeof(DataGrid),
                    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, OnDataContextChanged));
            }
    
            public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var grid = d as DataGrid;
                if (grid == null) return;
                foreach (var col in grid.Columns)
                    col.SetValue(FrameworkElement.DataContextProperty, e.NewValue);
            }
        }
    
        [ValueConversion(typeof(bool), typeof(Visibility))]
        public class VisibilityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value is bool && (bool)value)
                    return Visibility.Visible;
                return Visibility.Collapsed;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    
        public class MyItem
        {
            public string ColumnA { get; set; }
            public string ColumnB { get; set; }
            public string ColumnC { get; set; }
        }
    }
    

    I sourced this post by Jaime Rodriguez in creating my solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this to be bind to a datagrid as itemsource:
I want to bind one property to multiple sources. My reason for this are
I want to bind to a value in a dictionary property of an object.
I want to bind an event to a certain class and ID for when
I have a DataGrid and two StaticResource . I want to bind RowStyle of
In .net grid view Control we have a Property called DataPropertyName where we can
I'm trying to bind a List<List<string>> to a DataGrid programatically. I'm using this List<List<string>>
I want to to bind the entire row's background with a boolean property for
I want to bind a function to an event but I want to change
I want to bind a string value to a text box but only if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.