如果它包含某个值,如何使DataGrid列标题不可见

时间:2018-09-28 13:06:26

标签: c# wpf wpfdatagrid

我有一个DataGrid,如果它包含某个值,我是否可以使DataGridColumnHeader不可见。

这是我的尝试:

<DataGrid Grid.Row="1" Grid.Column="1" Width="400" HorizontalAlignment="Center" Margin="20" ItemsSource="{Binding RunNumbers.RunNumbers}" >
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Visibility" Value="Visible" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Content.Text, Mode=OneWay, RelativeSource={RelativeSource Self}}" Value="rows">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.ColumnHeaderStyle>
<DataGrid />

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如果将AutoGenerateColumns属性设置为false并定义自己要显式显示的列,则可以简单地避免设置某些列的Header属性,例如:< / p>

<DataGrid Grid.Row="1" Grid.Column="1" Width="400" HorizontalAlignment="Center" Margin="20" 
          ItemsSource="{Binding RunNumbers.RunNumbers}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <!-- RunNumbers.Column1 has no header... -->
        <DataGridTextColumn Binding="{Binding Column1}" />
    </DataGrid.Columns>
</DataGrid>

或者,您可以在视图中处理AutoGeneratingColumn事件:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "rows")
        e.Column.Header = string.Empty;
}

XAML:

<DataGrid Grid.Row="1" Grid.Column="1" Width="400" HorizontalAlignment="Center" Margin="20" 
          ItemsSource="{Binding RunNumbers.RunNumbers}" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" />