簡體   English   中英

WPF:根據 WPF Datagrid 中的列索引設置特定列的單元格的背景顏色

[英]WPF: Set background color of cells of a particular column based on column index in WPF Datagrid

我想在 WPF Datagrid 中設置特定列的所有單元格的背景顏色。

要求非常簡單明了,但我對 WPF 很陌生。

我發現了數百個類似Style Only One Column's Cells DataGrid C# WPF的帖子,它們使用DataGridCellDataTrigger組合設置特定單元格的樣式,但觸發器始終依賴於該單元格上的數據,而我不想依賴數據,但只是列索引。

有沒有辦法做到這一點?

您可以使用一個簡單的樣式和一個背景設置器(每列的單獨樣式):

<DataGridTextColumn Binding="{Binding ...}" Header="Red">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Green">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Green"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Blue">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Blue"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM