我有一个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 />
非常感谢您的帮助!
答案 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" />