Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to XAML therefore please pardon me for this basic question. Basically i am trying to change the color of the selected row in DataGrid. Currently when the row is selected the color is by default light blue but i dont understand where the color is set from. I want to change the selected color and also the text color of the selected row. Below is my code.

<DataGrid Name="TransferCallDataGrid"
             Margin="0 10 0 0"
             IsReadOnly="True"
             ItemsSource="{Binding Agents}"
             SelectedItem="{Binding SelectedAgent}"
             AutoGenerateColumns="False"
             HeadersVisibility="Column"
             HorizontalAlignment="Stretch"
             HorizontalGridLinesBrush="LightGray"
             VerticalGridLinesBrush="Transparent"
             HorizontalScrollBarVisibility="Disabled"
             CanUserAddRows="False"
             CanUserSortColumns="False"
             CanUserReorderColumns="False"
             BorderBrush="Transparent"
             SelectionUnit="FullRow"

             RowStyle="{DynamicResource DataGridRowStyle1}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}">
               <DataGrid.Columns>
                   <DataGridTemplateColumn Header="ID" Width=".7*">
                       <DataGridTemplateColumn.CellTemplate>
                           <DataTemplate>
                               <Grid>
                                   <Grid.ColumnDefinitions>
                                       <ColumnDefinition Width="2"/>
                                       <ColumnDefinition/>
                                   </Grid.ColumnDefinitions>
                                   <Rectangle Grid.Column="0" Fill="{Binding Path=AgentState, Converter={StaticResource AgentStateConverterResource}}" Width="3" Margin="0" HorizontalAlignment="Left"/>
                                   <TextBlock Grid.Column="1" TextAlignment="Center" Text="{Binding Id}" Style="{DynamicResource GridTextColumnStyle}"/>
                               </Grid>
                           </DataTemplate>
                       </DataGridTemplateColumn.CellTemplate>
                   </DataGridTemplateColumn>

                   <DataGridTemplateColumn Header="First Name" Width="*">
                       <DataGridTemplateColumn.CellTemplate>
                           <DataTemplate>
                               <TextBlock TextAlignment="Center" Text="{Binding FirstName}" Style="{DynamicResource GridTextColumnStyle}"/>
                           </DataTemplate>
                       </DataGridTemplateColumn.CellTemplate>
                   </DataGridTemplateColumn>

                   <DataGridTemplateColumn Header="Last Name" Width="*">
                       <DataGridTemplateColumn.CellTemplate>
                           <DataTemplate>
                               <TextBlock TextAlignment="Center" Text="{Binding LastName}" Style="{DynamicResource GridTextColumnStyle}"/>
                           </DataTemplate>
                       </DataGridTemplateColumn.CellTemplate>
                   </DataGridTemplateColumn>

                   <DataGridTemplateColumn Header="State" Width="1.1*">
                       <DataGridTemplateColumn.CellTemplate>
                           <DataTemplate>
                               <TextBlock TextAlignment="Center" Text="{Binding Path=AgentState, Converter={StaticResource AgentStateTextConverterResource}}" Style="{DynamicResource GridTextColumnStyle}" Foreground="{Binding Path=AgentState, Converter={StaticResource AgentStateConverterResource}}"/>
                           </DataTemplate>
                       </DataGridTemplateColumn.CellTemplate>
                   </DataGridTemplateColumn>
               </DataGrid.Columns>
           </DataGrid>


And following is the styling of the row i believe

<Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border" BorderBrush="Transparent" BorderThickness="0" Background="White" SnapsToDevicePixels="True">
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                            <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                        </SelectiveScrollingGrid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsNewItem" Value="True">
                <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
            </Trigger>

        </Style.Triggers>

    </Style>


What I have tried:

Have tried googling it and could not find much help there
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900