locked
How to set DataGridCell style on SelectedRow? Not entire selected row but one cell that is selected in row RRS feed

  • Question

  • Hi. I Use Microsoft.Windows.DataGrid.

    When I select a cell then entire row background is in blue and the selected cell has double borders. How can I set the cell style for the selected cell so the bordercolor would be something else?

    I have tried to use DataGrid.CellStyle but this applys to entire row. I need the selected/focused cell only. When I set SelectionUnit ="Cell" then it works but I need the SelectionUnit to remain FullRow. 

    Thursday, February 2, 2012 9:50 AM

Answers

  • Hi GaiusBaltar,

    In this case, you will need a MultiTriggle and set its Conditions as IsSelected and IsFocused. Thus, the style will only take effect on the selected cell.

             <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsFocused" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Pink" />
                                <Setter Property="Foreground" Value="Black" />
                                <Setter Property="BorderThickness" Value="1" />
                                <Setter Property="BorderBrush" Value="Yellow" />
                            </MultiTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
    

    Have a nice day,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by GaiusBaltar Tuesday, February 7, 2012 1:47 PM
    Monday, February 6, 2012 7:38 AM

All replies

  • You need to fiddle with both CellStyle and RowStyle. Both can can see IsSelected, so use triggers to do what you want.

    I haven't time to change it for what you need, but here's a copy/paste from one of my projects, that should help you get to where you need...

     

    <DataGrid.CellStyle> 
        <Style TargetType="DataGridCell"> 
            <Style.Triggers> 
                <Trigger Property="IsSelected" Value="True"> 
                    <Setter Property="Background" Value="White" /> 
                    <Setter Property="Foreground" Value="Black" /> 
                </Trigger> 
            </Style.Triggers> 
        </Style> 
    </DataGrid.CellStyle> 
    
    <DataGrid.RowStyle> 
        <Style TargetType="DataGridRow"> 
            <Style.Triggers> 
                <Trigger Property="IsSelected"  Value="True"> 
                    <Setter Property="BorderBrush" Value="Red" /> 
                    <Setter Property="BorderThickness"  Value="2" /> 
                </Trigger> 
            </Style.Triggers> 
        </Style> 
    </DataGrid.RowStyle> 
    

    Regards,
    Pedro

     


    If you find my post helpful, please remember to "Mark As Answer" AND/or "Mark as Helpful"

    Thursday, February 2, 2012 11:04 AM
  • Hi GaiusBaltar,

    In this case, you will need a MultiTriggle and set its Conditions as IsSelected and IsFocused. Thus, the style will only take effect on the selected cell.

             <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsFocused" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Pink" />
                                <Setter Property="Foreground" Value="Black" />
                                <Setter Property="BorderThickness" Value="1" />
                                <Setter Property="BorderBrush" Value="Yellow" />
                            </MultiTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
    

    Have a nice day,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by GaiusBaltar Tuesday, February 7, 2012 1:47 PM
    Monday, February 6, 2012 7:38 AM