根据其属性名称隐藏数据网格列 [英] Hide Datagrid Column based on its Property name

查看:24
本文介绍了根据其属性名称隐藏数据网格列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid 定义如下:

I have a DataGrid defined as follows :

<DataGrid Name="dtMydatagrid" Margin="10,10,10,10" RowHeight="20" AutoGenerateColumns="True" ItemsSource="{Binding}" Height="auto" Width="auto">
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <TextBox x:Name="TXT" Background="Transparent" Width="15" IsReadOnly="True" Visibility="Hidden" Margin="0,0,0,0"/>
               <DataTemplate.Triggers>
                  <DataTrigger Binding="{Binding Path=IsBKM}" Value="true">
                     <Setter Property="Background" Value="AQUA" TargetName="TXT"/>
                     <Setter Property="Visibility" Value="Visible" TargetName="TXT"/>
                  </DataTrigger>
               </DataTemplate.Triggers>
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
   </DataGrid.Columns>
</DataGrid>

现在,我的类中有一个名为 IsBKM 的布尔属性,DataGridTemplateColumn 绑定到该属性.因此,它显示为 CheckBox.我不想在我的 DataGrid 中显示 IsBKM 列.我可以使用触发器并隐藏名称为 IsBKM任何不同解决方案的列吗?

Now, I have a boolean property in my class named IsBKM to which the DataGridTemplateColumn is bounded. So, it is displayed by as CheckBox. I don't want to display the IsBKM column in my DataGrid. Can I use a trigger and hide the column whose name is IsBKM or any different solution?

提前致谢.

推荐答案

您可以处理 DataGrid.AutoGeneratedColumns 事件 并从那里设置列的 Visibility 属性.你应该能够做这样的事情:

You could handle the DataGrid.AutoGeneratedColumns Event and set the column's Visibility property from there. You should be able to do something like this:

private void DataGridAutoGeneratingColumn(object sender, 
    DataGridAutoGeneratingColumnEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    if (dataGrid != null && IsBKM) dataGrid.Columns[0].Visible = false;
}

<小时>

更新>>>


UPDATE >>>

您可以使用 e.Column.Header 属性来检查列的名称,然后改用它.但是,您的列当前没有设置 Header.您还可以设置列名(在 XAML 中),然后检查该 Name 值,而不是使用 Header 属性:

You can use the e.Column.Header property to check the name of the column and then use that instead. However, your column has no Header currently set. You could also set the column name (in XAML) and then check for that Name value instead of using the Header property:

private void DataGridAutoGeneratingColumn(object sender, 
    DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Name == "IsBKM" && IsBKM)
    {
        e.Column.Visibility = Visibility.Collapsed;
    }
}

这篇关于根据其属性名称隐藏数据网格列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆