簡體   English   中英

如何更改數據網格的背景色

[英]how to change the background color of the datagrid

我如何更改我的數據網格的顏色,我卻沒有在設計器中使用的顏色,但是當我調試它時,它只是變回默認的白色,我已經嘗試過了

                <DataGrid.Resources>
                    <Style TargetType="{x:Type DataGrid}">
                        <Setter Property="Background" Value="#FF303030"/>
                    </Style>
                </DataGrid.Resources>

但這沒有用

您只需要定義一次背景色。 如果您還在DataGrid聲明中進行設置,它將覆蓋樣式。

例如,在第一個示例中,我在聲明中將背景明確設置為紅色,因此它忽略了樣式中的顏色。 結果=紅色背景

    <DataGrid Background="Red">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGrid}">
                <Setter Property="Background" Value="Green"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

而如果我從聲明中刪除顏色,它將從樣式中選取顏色。 在第二個示例中,您將看到綠色背景。

    <DataGrid>
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGrid}">
                <Setter Property="Background" Value="Green"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

請注意, DataGridBackground是單元格后面的顏色(如果DataGrid充滿數據,您甚至可能看不到它)。 如果要更改這些顏色,則可能還需要設置DataGridRowDataGridRowHeaderDataGridColumnHeader樣式。 我還包括了用於設置左上角全選按鈕的樣式 ,這有點棘手,摘自Style datagrid表-左上角

    <DataGrid>
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGrid}">
                <Setter Property="Background" Value="Green"/>
            </Style>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="Blue"/>
            </Style>
            <Style TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Background" Value="Yellow"/>
            </Style>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="Orange"/>
            </Style>
            <Style TargetType="{x:Type Button}" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
                <Setter Property="Background" Value="Black" />
            </Style>
        </DataGrid.Resources>
    </DataGrid>

暫無
暫無

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

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