简体   繁体   English

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

[英]Binding programmatically generated DataGridTextColumn's Visibility property to a checkbox

I'm trying to bind the visibility property of a DataGridTextColumn to the IsChecked value of a combo box (cbIP). 我正在尝试将DataGridTextColumn的visibility属性绑定到组合框(cbIP)的IsChecked值。 For most columns, I have solved the problem in XAML with lines like this: 对于大多数列,我用XALL解决了这样的问题:

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

However, some columns regard values that are based on arrays of varying length (and therefore varying number of columns). 但是,某些列会考虑基于不同长度(因此列数不同)的数组的值。 This I have no problem creating in the code behind. 这个我在后面的代码中创建没有问题。 The only problem is the visibility property. 唯一的问题是可见性属性。 I have come this 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, it doesn't work. 不用说,它不起作用。 I see the columns, but the checkbox doesnt work. 我看到列,但复选框不起作用。 (It works for the XAML-generated columns. (适用于XAML生成的列。

What do I do wrong? 我做错了什么?

Thanks in advance! 提前致谢!

I work the other way -> Try This where the list of columns are used to generate the check boxes, and then toggle the visiblity.. 我的工作方式是另一种方式 - >试试这个列的列用于生成复选框,然后切换visiblity ..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM