wpf DataGrid DataGrid.Columns背景颜色
时间: 2024-06-15 13:00:47 浏览: 17
WPF DataGrid是一种用于显示数据的控件,而DataGrid.Columns则是DataGrid中的列集合。你可以通过设置DataGrid.Columns的Background属性来改变整个列的背景颜色。
在WPF中,你可以使用多种方式来设置DataGrid.Columns的Background属性,包括使用静态资源、绑定属性、使用样式等。以下是一些示例代码:
1. 使用静态资源设置DataGrid.Columns的Background属性:
```
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" Binding="{Binding Column1}">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="{StaticResource MyBrush}" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
```
2. 使用绑定属性设置DataGrid.Columns的Background属性:
```
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" Binding="{Binding Column1}">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="{Binding DataContext.Column1HeaderBackground, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
```
3. 使用样式设置DataGrid.Columns的Background属性:
```
<DataGrid>
<DataGrid.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="LightGray" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" />
</DataGrid.Columns>
</DataGrid>
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)