简体   繁体   中英

How to change Background of datagridcell based on multiple condition WPF?

I have datagrid with some conditional row. And I want if I select particular row in datagrid, The selected color will change based on my record value. Suppose I have record of Students with IsDropOut Flag.

And then If I click selected record of student, Selected row will change color according to IsDropOut value. If it is true it will set selected row background to red and if IsDropOut is false it will change selected row background to green.

Maybe with like this

       <DataGrid.Resources>

                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding Path=DataGridCell.IsSelected}" Value="True" />
                                    <Condition Binding="{Binding Path=IsDropOut}" Value="true" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Background" Value="red" />
                            </MultiDataTrigger>

                        </Style.Triggers>
                    </Style>
                </DataGrid.Resources>

But that's code is not work. So how to do it? Anyone has suggestion?

Finally I solve this.. Thanks for the idea..

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Path=IsDropOut}" Value="True" />
        <Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" Value="Red" />
    <Setter Property="Foreground" Value="White" />
</MultiDataTrigger>

Try this :

          <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="True" />
                            <Condition Property="IsDropOut" Value="true" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="Red" />
                    </MultiTrigger>
                </Style.Triggers>
            </Style>

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