将以编程方式生成的DataGridTextColumn的Visibility属性绑定到复选框

时间:2013-03-25 00:17:53

标签: c# wpf binding datagrid visibility

我正在尝试将DataGridTextColumn的visibility属性绑定到组合框(cbIP)的IsChecked值。对于大多数专栏,我用XALL解决了这样的问题:

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

但是,有些列会考虑基于不同长度(因此列数不同)的数组的值。这个我在后面的代码中创建没有问题。唯一的问题是可见性属性。我走到这一步:

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);              
            }          
        }  

不用说,它不起作用。我看到列,但复选框不起作用。 (适用于XAML生成的列。

我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我以另一种方式工作 - &gt;尝试使用列表列表生成复选框,然后切换可见性..

<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>