KMC
KMC

Reputation: 20046

How to un-highline WPF DataGrid row?

I have a WPF DataGrid and I need to select some rows in the DataGrid. After done selecting, the blue highline remains on the row, and I can't figure out how to get rid of it. I tried clicking somewhere else but the blue-highline remains on the row.

enter image description here

Upvotes: 0

Views: 1191

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81243

You can handle the event IsKeyboardFocusWithinChanged in your code behind file to set the SelectedItem to null like this -

private void dg_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
   if (!(bool)e.NewValue)
   {
       (sender as DataGrid).SelectedItem = null;
   }
}

Xaml file:

<DataGrid x:Name="dg" IsKeyboardFocusWithinChanged="dg_IsKeyboardFocusWithinChanged"/>

In case you dont want the selection border in your datagrid, you need to override System.HighlightBrush and add it to your datagrid Resources like this -

<DataGrid>
    <DataGrid.Resources>
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
    </DataGrid.Resources>
<DataGrid>

Upvotes: 1

Jin-Wook Chung
Jin-Wook Chung

Reputation: 4324

Although I do not know what exactly you want to do, you can control the the highline with redefining DataGridCell and DataGridRow styles. I'll show you an example, which may or may not be the thing you want to do. I wish it helps with you.

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Style.Triggers>
                <Trigger Property="IsSelected"
                        Value="True">
                    <Setter Property="BorderThickness"
                        Value="0" />
                </Trigger>
                <Trigger Property="IsFocused"
                        Value="False">
                    <Setter Property="Background"
                        Value="Transparent" />
                    <Setter Property="Foreground"
                        Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <DockPanel>
        <TextBox DockPanel.Dock="Top"></TextBox>
        <DataGrid ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="First Name" Binding="{Binding}" />
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</Window>

Upvotes: 0

Related Questions