0

I'm working on a WPF project where I have a main window. This main window allows you to choose between some options. The idea is that in the main window you can choose which option you want to open. Every option is a different tab with a datagrid inside. All of the tabs share the same columns, except for one of them (called "special"), which has a column in addition. What I want is to include this additional column in the general datagrid, and bind it to a visibility property so that it only shows up when it is in the "special" tab.

This is the code of the additional column in XAML:

<DataGridTemplateColumn
   x:Name="Special"
   Header="Special Tab"
   Width="0.5*"
   Visibility="{Binding PropertyVisibilitySpecial, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
         <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <Grid>
                      <CheckBox 
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           IsChecked="{Binding PropertySpecial, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                           Checked="CheckBoxChanged" Unchecked="CheckBoxChanged">
                          </CheckBox>
                 </Grid>
            </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

The next thing I have is the ViewModel, where I have defined the visibility property (PropertyVisibilitySpecial), and a constructor.

The property has been defined as follows:

    Visibility propertyVisibilitySpecial;

    public System.Windows.Visibility PropertyVisibilitySpecial
    {
        get { return propertyVisibilitySpecial; }
        set
        {
            propertyVisibilitySpecial = value;
            MessageBox.Show(PropertyVisibilitySpecial.ToString());
            OnPropertyChanged("PropertyVisibilitySpecial");
        }
  
    }

(As you can note, there's a message box inside the property. I've done this to make a trace of when the property changes, because I was worried that the property could be changing in another part of the code). Thanks to the MessageBox, I've been able to check that it only changes once, in the constructor)

The constructor of the ViewModel:

public ViewModel(Model model) 
{
   this.model = model;
   this.view = new View();
   view.DataContext = this;
   
   PropertyVisibilitySpecial = System.Windows.Visibility.Hidden;
}

That's all. The problem is that the additional column appears now in every tab/option. That makes no sense, since the visibility of the column now is initialized as Hidden, it should not be visible in any tab. (For the record, I've initialized it as hidden just to test it, my plans are to insert there a conditional to change the visibility according to other conditions)

Does anyone know how to fix this?

Thank you so much in advance.

JohnG
  • 9,259
  • 2
  • 20
  • 29
Laura
  • 15
  • 6
  • Does this answer your question? [Show/Hide DataGrid Columns XAML](https://stackoverflow.com/questions/30621672/show-hide-datagrid-columns-xaml) – Orace Mar 23 '21 at 10:53
  • No... now I have a new error: System.Windows.Markup.XamlParseException. basically it says that it doesn't recognize the PropertyVisibilitySpecial anymore. – Laura Mar 23 '21 at 11:50
  • This is what I've written: Visibility="{Binding Source={x:Reference PropertyVisibilitySpecial}, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"> – Laura Mar 23 '21 at 11:56
  • Remove this UpdateSourceTrigger=PropertyChanged never use it again until you understand what it is. A datagrid column is an abstract thing. Run your app and use the visual tree tool or snoop to look at what you have. There is no such thing in there as a column. Next. Search before you ask a question. Because this has already been asked and your question will therefore be closed. https://stackoverflow.com/questions/22073740/binding-visibility-for-datagridcolumn-in-wpf – Andy Mar 23 '21 at 18:35
  • Does this answer your question? [Binding Visibility for DataGridColumn in WPF](https://stackoverflow.com/questions/22073740/binding-visibility-for-datagridcolumn-in-wpf) – Andy Mar 23 '21 at 18:35

0 Answers0