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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:47:20+00:00 2026-05-24T18:47:20+00:00

I have a ListView with some GridViewColumns and I’d like to be able to

  • 0

I have a ListView with some GridViewColumns and I’d like to be able to show or hide them through checkable items in a ContextMenu.

MainView.xaml:

<ListView>
  <ListView.ContextMenu>
    <ContextMenu>
      <MenuItem x:Name="cma" Header="a" IsCheckable="True" IsChecked="True"/>
      <MenuItem x:Name="cmb" Header="b" IsCheckable="True"/>
      <MenuItem x:Name="cmc" Header="c" IsCheckable="True" IsChecked="True"/>
      <MenuItem x:Name="cmd" Header="d" IsCheckable="True"/>
    </ContextMenu>
  </ListView.ContextMenu>
  <ListView.View>
    <GridView>
      <c:GridViewColumnExt c:GridViewColumnExt.IsVisible="{Binding ElementName=cma, Path=IsChecked}">
        <GridViewColumnHeader Content="a"/>
      </c:GridViewColumnExt>
      <c:GridViewColumnExt c:GridViewColumnExt.IsVisible="{Binding ElementName=cmb, Path=IsChecked}">
        <GridViewColumnHeader Content="b"/>
      </c:GridViewColumnExt>
      <c:GridViewColumnExt c:GridViewColumnExt.IsVisible="{Binding ElementName=cmc, Path=IsChecked}">
        <GridViewColumnHeader Content="c"/>
      </c:GridViewColumnExt>
      <c:GridViewColumnExt c:GridViewColumnExt.IsVisible="{Binding ElementName=cmd, Path=IsChecked}">
        <GridViewColumnHeader Content="d"/>
      </c:GridViewColumnExt>
    </GridView>
  </ListView.View>
</ListView>

GridViewColumnExt.cs:

public class GridViewColumnExt : GridViewColumn
{
  private double _visibleWidth = double.NaN;

  public bool IsVisible
  {
    get { return (bool)GetValue(IsVisibleProperty); }
    set { SetValue(IsVisibleProperty, value); }
  }

  public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.Register("IsVisible", typeof(bool), typeof(GridViewColumnExt), new FrameworkPropertyMetadata(true, OnIsVisibleChanged));

  private static void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    var newValue = bool.Parse(e.NewValue.ToString());
    var column = (GridViewColumnExt)d;
    var header = (GridViewColumnHeader)column.Header;
    header.IsEnabled = newValue;
    if (newValue)
    {
      column.Width = column._visibleWidth;
      header.IsEnabled = true;
      header.Visibility = Visibility.Visible;
    }
    else
    {
      column._visibleWidth = column.Width;
      column.Width = 0;
      header.IsEnabled = false;
      header.Visibility = Visibility.Collapsed;
    }
  }
}

MainViewModel.cs (Never mind, the solution doesn’t need this)

The problem:

  • When the window is first shown, columns a and c are visible
  • When the ContextMenu is first shown, columns a and c become hidden and only column b is shown. Also, only column b is checked in the ContextMenu

Does anyone know what causes this?


Solution:

Removed binding to ViewModel properties and and just set the MenuItem.IsChecked to a default value.
By the way, if anyone has an even better way of handling this kind of stuff, please let me know 🙂

  • 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-24T18:47:22+00:00Added an answer on May 24, 2026 at 6:47 pm

    FallbackValue is for when a binding is unable to return a value. Column b is checked and visible because you’re setting it to true in the constructor. Update your constructor appropriately.

    public class MainViewModel
    {
      public MainViewModel()
      {
        ColumnChecked_a = true;
        ColumnChecked_b = true;
        ColumnChecked_c = true;
      }
    }
    

    Also, I’m thinking the columns’ visibility changes because the contextmenu maybe isn’t “created” until the right-click. Try a normal menu and see if that works, yeah.

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

Sidebar

Related Questions

I have a listview with some items. I would like to get the text
I have a listView vith some items. I would like to get from my
I have seen some ListView dynamic growing implementations which use OnScrollListener. I would like
I have a listview with some items that can be marked as done. There
I have a ListView containing some ListViewItems. By default, selecting items makes their background
I have listview with some listadapter after click on item from list I'd like
I have a ListView with some info like Eifell Tower, Louvre and places like
I have Android ListView that holding some items, the flow that I'am trying to
I have ListView with about 30 items. When I click on an item, some
I have a ListView with multiple choice entries where some are checked from the

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.