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 1081703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:12:08+00:00 2026-05-16T22:12:08+00:00

I have an issue similar to the following post: Silverlight DataGridTextColumn Binding Visibility I

  • 0

I have an issue similar to the following post:

Silverlight DataGridTextColumn Binding Visibility

I need to have a Column within a Silverlight DataGrid be visibile/collapsed based on a value within a ViewModel. To accomplish this I am attempting to Bind the Visibility property to a ViewModel. However I soon discovered that the Visibility property is not a DependencyProperty, therefore it cannot be bound.

To solve this, I attempted to subclass my own DataGridTextColumn. With this new class, I have created a DependencyProperty, which ultimately pushes the changes to the DataGridTextColumn.Visibility property. This works well, if I don’t databind. The moment I databind to my new property, it fails, with a AG_E_PARSER_BAD_PROPERTY_VALUE exception.

public class MyDataGridTextColumn : DataGridTextColumn
{
    #region public Visibility MyVisibility

    public static readonly DependencyProperty MyVisibilityProperty =
        DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(MyDataGridTextColumn), new PropertyMetadata(Visibility.Visible, OnMyVisibilityPropertyChanged));

    private static void OnMyVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var @this = d as MyDataGridTextColumn;

        if (@this != null)
        {
            @this.OnMyVisibilityChanged((Visibility)e.OldValue, (Visibility)e.NewValue);
        }
    }

    private void OnMyVisibilityChanged(Visibility oldValue, Visibility newValue)
    {
        Visibility = newValue;
    }

    public Visibility MyVisibility
    {
        get { return (Visibility)GetValue(MyVisibilityProperty); }
        set { SetValue(MyVisibilityProperty, value); }
    }

    #endregion public Visibility MyVisibility
}

Here is a small snippet of the XAML.

<DataGrid ....>
    <DataGrid.Columns>
        <MyDataGridTextColumn Header="User Name"
                              Foreground="#FFFFFFFF"
                              Binding="{Binding User.UserName}"
                              MinWidth="150"
                              CanUserSort="True"
                              CanUserResize="False"
                              CanUserReorder="True"
                              MyVisibility="{Binding Converter={StaticResource BoolToVisibilityConverter}, Path=ShouldShowUser}"/>
        <DataGridTextColumn .../>
    </DataGrid.Columns>
</DataGrid>

A couple important facts.

  • The Converter is indeed defined above in the local resources.
  • The Converter is correct, it is used many other places in the solution.
  • If I replace the {Binding} syntax for the MyVisibility property with “Collapsed” the Column does in fact disappear.
  • If I create a new DependencyProperty (i.e. string Foo), and bind to it I receive the AG_E_PARSER_BAD_PROPERTY_VALUE exception too.

Does anybody have any ideas as to why this isn’t working?

  • 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-16T22:12:08+00:00Added an answer on May 16, 2026 at 10:12 pm

    Here’s the solution I’ve come up with using a little hack.

    First, you need to inherit from DataGrid.

    public class DataGridEx : DataGrid
    {
        public IEnumerable<string> HiddenColumns
        {
            get { return (IEnumerable<string>)GetValue(HiddenColumnsProperty); }
            set { SetValue(HiddenColumnsProperty, value); }
        }
    
        public static readonly DependencyProperty HiddenColumnsProperty =
            DependencyProperty.Register ("HiddenColumns", 
                                         typeof (IEnumerable<string>), 
                                         typeof (DataGridEx),
                                         new PropertyMetadata (HiddenColumnsChanged));
    
        private static void HiddenColumnsChanged(object sender,
                                                 DependencyPropertyChangedEventArgs args)
        {
            var dg = sender as DataGrid;
            if (dg==null || args.NewValue == args.OldValue)
                return;
    
            var hiddenColumns = (IEnumerable<string>)args.NewValue;
            foreach (var column in dg.Columns)
            {
                if (hiddenColumns.Contains ((string)column.GetValue (NameProperty)))
                    column.Visibility = Visibility.Collapsed;
                else
                    column.Visibility = Visibility.Visible;
            }
        }
    }
    

    The DataGridEx class adds a new DP for hiding columns based on the x:Name of a DataGridColumn and its descendants.

    To use in your XAML:

    <my:DataGridEx x:Name="uiData"
                   DataContext="{Binding SomeDataContextFromTheVM}"
                   ItemsSource="{Binding Whatever}"
                   HiddenColumns="{Binding HiddenColumns}">
        <sdk:DataGridTextColumn x:Name="uiDataCountOfItems">
                                Header="Count"
                                Binding={Binding CountOfItems}"
        </sdk:DataGridTextColumn>
    </my:DataGridEx>
    

    You need to add these to your ViewModel or whatever data context you use.

    private IEnumerable<string> _hiddenColumns;
    public IEnumerable<string> HiddenColumns
    {
        get { return _hiddenColumns; }
        private set
        {
            if (value == _hiddenColumns)
                return;
    
            _hiddenColumns = value;
            PropertyChanged (this, new PropertyChangedEventArgs("HiddenColumns"));
        }
    }
    
    public void SomeWhereInYourCode ()
    {
        HiddenColumns = new List<string> {"uiDataCountOfItems"};
    }
    

    To unhide, you only need to remove the corresponding name from the list or recreate it without the unhidden name.

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

Sidebar

Related Questions

Following on from this question about setting Culture, I have a similar issue with
I have a similar issue like here: http://social.msdn.microsoft.com/forums/en-US/biztalkgeneral/thread/87d5a6ec-04ee-4c6f-8267-f526ee105f0b I have an asp.net web page
Before anyone jumps on me, I have found a similar issue here , but
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue using the PEAR libraries on Windows from PHP . Pear
We have an issue related to a Java application running under a (rather old)
We have an issue on our page whereby the first time a button posts
I have some issue with a Perl script. It modifies the content of a
I have an issue with using AWK to simply remove a field from a
I have big issue with url-rewriting for IIS 7.0. I've written simple module for

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.