0
votes

I have WPF form with a DataGrid in it. When the form loads or when it doesn't have focus, there is a white colored highlight on the first cell. If I click on the gridview or when the form gets focus, the current cell becomes blue (the selection color that I set). See pictures:

'Lost Focus enter image description here

Got Focus enter image description here

I want to remove the white highlighting when there is no focus on the datagrid, i.e. it should look like the other cells in that row. What is the property or event that I must handle?

        <DockPanel Name="DocHolder" >            
           <DataGrid x:Name="dataGrid1" MaxHeight="100" AutoGenerateColumns="False" HeadersVisibility="Column" SelectionUnit="Cell" FontSize="11" 
                 VerticalContentAlignment="Center" 
                  HorizontalGridLinesBrush="#FFC7BDBD" AlternatingRowBackground="#690BB9F8"
                  EnableColumnVirtualization="False" EnableRowVirtualization="False" GridLinesVisibility="None"
                CanUserResizeColumns="False"  CanUserResizeRows="False"  CanUserReorderColumns="False" CanUserAddRows="False"  CanUserDeleteRows="False"  
                ClipboardCopyMode="ExcludeHeader" HorizontalScrollBarVisibility="Disabled"  MinRowHeight="20"  OverridesDefaultStyle="False"
                Style="{DynamicResource scGrid}" ColumnHeaderStyle="{DynamicResource scGridHeader}">

                <DataGrid.BorderBrush> <SolidColorBrush /> </DataGrid.BorderBrush>
                <DataGrid.RowBackground> <SolidColorBrush /></DataGrid.RowBackground>
                <DataGrid.Background> <SolidColorBrush /> </DataGrid.Background>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="GID"  DisplayIndex="0" Visibility="Hidden"   Binding="{Binding GID}" />
                    <DataGridTextColumn Header="Region" DisplayIndex="1" IsReadOnly="True" Binding="{Binding AreaName}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
                    <DataGridTextColumn Header="Grps"  DisplayIndex="2" IsReadOnly="True" Binding="{Binding GName}" CellStyle="{StaticResource FocusAndShowElementData}" HeaderStyle="{StaticResource HeaderStyle}" />
                    <DataGridTextColumn Header="Thk"   DisplayIndex="3" Binding="{Binding Thick}"  IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}"  HeaderStyle="{StaticResource HeaderStyle}"/>
                    <DataGridTextColumn Header="TX"   DisplayIndex="4" Binding="{Binding TopX}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
                    <DataGridTextColumn Header="TY"   DisplayIndex="5" Binding="{Binding TopY}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
                    <DataGridTextColumn Header="BX"  DisplayIndex="6" Binding="{Binding BottomX}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}"  HeaderStyle="{StaticResource HeaderStyle}"/>
                    <DataGridTextColumn Header="BY"   DisplayIndex="7" Binding="{Binding BottomY}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
                    <DataGridComboBoxColumn Header="DTX"  DisplayIndex="8" TextBinding ="{Binding BTopX, Mode=TwoWay}" x:Name="cmbBTopX" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
                    <DataGridTextColumn Header="STX"  DisplayIndex="9" Binding="{Binding BTopX, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
                    <DataGridComboBoxColumn Header="DTY"  DisplayIndex="10" TextBinding ="{Binding BTopY, Mode=TwoWay}" x:Name="cmbBTopY" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
                    <DataGridTextColumn Header="STY"   DisplayIndex="11" Binding="{Binding BTopY, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
                    <DataGridComboBoxColumn Header="DBX"  DisplayIndex="12" TextBinding ="{Binding BBottomX, Mode=TwoWay}"  x:Name="cmbBBottomX" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
                    <DataGridTextColumn Header="SBX" DisplayIndex="13" Binding="{Binding SBottomX, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
                    <DataGridComboBoxColumn Header="DBY"  DisplayIndex="14" TextBinding ="{Binding BBottomY, Mode=TwoWay}" x:Name="cmbBBottomY" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
                    <DataGridTextColumn Header="SBY"  DisplayIndex="15" Binding="{Binding BBottomY, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
            </DataGrid.Columns>               
        </DataGrid>            
    </DockPanel>                       
1
you have to override the datagrid cell template. - Frebin Francis

1 Answers

1
votes

You have to override the cell template of datagrid cell

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="False">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center"></ContentPresenter>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This is just an example that you can refer, Like this you can do whatever template you want

Hope this helps.