Associating a software-generated DataGridTextColumn Visibility property with a checkbox

I am trying to associate the visibility property DataGridTextColumn with the value IsChecked in a combo box (cbIP). For most columns, I solved the problem in XAML with lines like this:

<DataGridTextColumn Header="Time" Binding="{Binding MeasureTime}" Visibility="{Binding Source={x:Reference cbMeasureTime}, Path=IsChecked, Converter={StaticResource BoolToVisConverter}}"></DataGridTextColumn> 

However, some columns relate to values ​​that are based on variable-length arrays (and therefore different amounts of columns). I have no problem creating code. The only problem is the visibility property. I went so far:

 private void Page_Loaded_1(object sender, RoutedEventArgs e) { for (int i = 0; i < ds.NumberOfIPValues; i++) { DataGridTextColumn col = new DataGridTextColumn() { Header = String.Format("IP #{0} (mV/V)", i + 1) }; col.Binding = new Binding(String.Format("IP[{0}]",i)); Binding b = new Binding("Visibility"); b.Source = cbIP; b.Path = new PropertyPath(typeof(CheckBox).GetProperty("IsChecked")); b.Converter = new BoolToVisibilityConverter(); BindingOperations.SetBinding(col, DataGridTextColumn.VisibilityProperty, b); ViewInTableDataGrid.Columns.Add(col); } } 

Needless to say, this will not work. I see the columns, but the checkbox does not work. (It works for columns created by XAML.

What am I doing wrong?

Thanks in advance!

+4
source share
1 answer

I work in another way -> Try This, where the column list is used to create checkboxes, and then toggles the visibility.

 <ItemsControl ItemsSource="{Binding Path=Columns, ElementName=dgSearchResult, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}" > <ItemsControl.ItemTemplate> <DataTemplate > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="5"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <CheckBox Content="{Binding Path=Header}" IsChecked="{Binding Path=Visibility, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=TwoWay, Converter={StaticResource BooleanToHiddenConvertor}}" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> 

0
source

All Articles