简体   繁体   中英

DataGrid.CellStyle applies to whole row in C# WPF App

I am currently working on a small c# wpf project. I fill my datagrid programmatically and I want to change one cells color in the style without affecting the whole row. I do this after changing the DataGrid Rowstyle.

                   <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow">

                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Gebucht}" Value="True">
                                    <Setter Property="Background" Value="Green"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=dringendeBuchung}" Value="True">
                                    <Setter Property="Background" Value="Yellow"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                        
                    </DataGrid.RowStyle>
                    <DataGrid.CellStyle>
                        <Style TargetType="DataGridCell">

                            <Style.Triggers>

                                <DataTrigger Binding="{Binding Path=Dringend}" Value="True">
                                    <Setter Property="Background" Value="Red"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.CellStyle>

The rowstyle works fine, but if for example "Gebucht" is true and Dringend is true too I want to display the whole row in green except for the Cell which has the Dringend boolean.

Thank you for your help

The rowstyle works fine, but if for example "Gebucht" is true and Dringend is true too I want to display the whole row in green except for the Cell which has the Dringend boolean.

Then you should set the CellStyle property of the specific column instead of setting the CellStyle property for the entire DataGrid , eg:

<DataGrid ... >
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Gebucht}" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=dringendeBuchung}" Value="True">
                    <Setter Property="Background" Value="Yellow"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Dringend}" Header="Dringend">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Dringend}" Value="True">
                            <Setter Property="Background" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
        <!-- + other columns... -->
    </DataGrid.Columns>
</DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM