Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

WPF: Change Selected Row Color

 

How to change the Selected line of a data grid in color?

Solution

For this purpose, you can customize the DataGridRow via a style.

Then you select the trigger on IsSelected and you can choose the background color on the chosen color

 

 

<DataGrid SelectionMode="Single"           

SelectionUnit="FullRow">

            <DataGrid.RowStyle>

                <Style TargetType="DataGridRow">

                    <Style.Triggers>

                        <Trigger Property="IsSelected" Value="True">

                            <Setter Property="Background" Value="Blue"/>

                        </Trigger>

                    </Style.Triggers>

                </Style>

            </DataGrid.RowStyle>

        </DataGrid>

 

 

Full WPF XAML Grid

 

<DataGrid x:Name="ctlDataGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"

          AutoGenerateColumns="False"

          GridLinesVisibility="All"

          Background="#F0f0f0" 

          SelectionMode="Single" 

          IsReadOnly="True" 

          MouseDoubleClick="ctlDataGrid_MouseDoubleClick"

           SelectionChanged="CtlDataGrid_SelectionChanged"

           HeadersVisibility="All"                  

           Grid.Row="1" 

           SelectionUnit="FullRow"                   

          >

            <DataGrid.RowStyle>

                <Style TargetType="DataGridRow">

                    

                    <Style.Triggers>

                        <Trigger Property="IsSelected" Value="True">

                            <Setter Property="Background" Value="Blue"/>

                        </Trigger>

                    </Style.Triggers>

                </Style>

            </DataGrid.RowStyle>

 

            <DataGrid.Columns>

                <DataGridTextColumn Header="ID" Binding="{Binding IDFeiertag}"/>

                <DataGridTextColumn Header="Datum" Binding="{Binding Datum_Feiertag}" Width="90"/>

                <DataGridTextColumn Header="Feiertag" Binding="{Binding Feiertag}" Width="140"/>

                <DataGridTextColumn Header="Kurz" Binding="{Binding Kurz}">

                    <DataGridTextColumn.CellStyle>

                        <Style TargetType="DataGridCell">

                            <Setter Property="Background" Value="{Binding Farbe}" />

                            <Setter Property="Foreground" Value="{Binding Textfarbe}" />

                        </Style>

                    </DataGridTextColumn.CellStyle>

                </DataGridTextColumn>

 

 

            </DataGrid.Columns>

        </DataGrid>

 

 

Mobile
»
WPF Datagrid: Set Color of Row when it has no Focus
»
WPF Datagrid: Change the Color of Selected Row
»
WPF: Change Selected Row Color in DataGrid
»
WPF: Read SelectedItem from DataGrid with Dynamic As Anonymous Type
»
WPF: Elements too wide, edge Cut off
»
Solved: System.InvalidCastException HResult 0x80004002 in Select DataGrid
»
WPF: Combobox Selected Value
»
WPF Datagrid: Column is not displayed
»
WPF: Show video in preview
»
WPF Webbrowser: Suppress Java Errors or Set WebBrowser to Silent

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de