Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1310
Set Visible or Collapse to a column of a ColumnGroup. Issue.
posted

Hello,

I have been going around for a way of hiding a column by using a combobox, trying to set the way of automatically when the user chooses from the combobox one option, to show the corresponding column on the grid and hide the other ones.

By binding the Visibility option directly to my ViewModel didn't work, so I have developped a solution that works at 50% in the code behind of my view.

I have attached the propertyChanged of my ViewModel to my view's code behind, so each event occured in my viewModel I will be notified about. So, when the user chooses from the combobox the option of Month, I want to show only the column of Year, the same happens when user chooses Day, I will show only the column Day, and not the Month, and the third option is Both, that means everything is going to be visible.

The concept is clear and easy, but when I have tested the solution shown at the end, it happens that if I do:

Both, then Month (the grid shows only Months) and afterwards Both (grid shows months and days), everything goes perfect, but if I do:

Both, then Months(months are shown and days hidden) and afterwards Days, the grid hides everything instead of showing back Days. The same happens if I do Both, Days and the last one Months.

Once all the grid is hidden, even if I choose Both, nothing is displayed.

How can be this possible?

Here my view's code behind

 private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var listOfColumns = dataGrid.Columns.AllColumns;
            if (e.PropertyName.Equals("MonthsVisibility"))
            {
                foreach (var column in listOfColumns)
                {
                    if (column.Tag != null)
                    {
                        var columnTag = column.Tag.ToString();
                        if (columnTag.Contains("Month"))
                        {
                            column.Visibility = ((ViewModel)DataContext).MonthsVisibility;
                        }
                    }
                }
            }
            else if (e.PropertyName.Equals("DaysVisibility"))
            {
                foreach (var column in listOfColumns)
                {
                    if (column.Tag != null)
                    {
                        var columnTag = column.Tag.ToString();
                        if (columnTag.Contains("Day"))
                        {
                            column.Visibility = ((ViewModel)DataContext).DaysVisibility;
                        }
                    }
                }
            }
        }

Here my ViewModel code:

 private void ShowSelectedOption()         {             switch (SelectedOption)             {                 case OptionsToShow.Both:                     DaysVisibility = Visibility.Visible;                     MonthsVisibility = Visibility.Visible;                     break;                 case OptionsToShow.Months:                     DaysVisibility = Visibility.Collapsed;                     MonthsVisibility = Visibility.Visible;                     break;                 case OptionsToShow.Days:                     DaysVisibility = Visibility.Visible;                     MonthsVisibility = Visibility.Collapsed;                     break;             }         }

And in my view:
 <ig:GroupColumn.Columns> <ig:TextColumn Key="Month"        Tag="Month"                          HorizontalContentAlignment="Right"          AllowEditingValidation="True"              IsHideable="True"             >                                     <ig:TextColumn.HeaderTemplate>                                         <DataTemplate>                                             <TextBlock Text="Month" />                                         </DataTemplate>                                     </ig:TextColumn.HeaderTemplate>                                 </ig:TextColumn>                                 <ig:TextColumn Key="Day"                                                Tag="Day"                                                                                               HorizontalContentAlignment="Right"                                                IsHideable="True">                                     <ig:TextColumn.HeaderTemplate>                                         <DataTemplate>                                             <TextBlock Text="Day" />                                         </DataTemplate>                                     </ig:TextColumn.HeaderTemplate>                                 </ig:TextColumn>
 </ig:GroupColumn.Columns>
  • 138253
    Offline posted

    Hello,

     

    Thank you for your post. I have been looking into it, but it seems like am missing something about your scenario, so if this is still an issue for you, could you please send me an isolated sample project, where this is reproduced, so I can investigate it further for you.

     

    Looking forward for your reply.