开发者

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 c开发者_Go百科licking somewhere else but the blue-highline remains on the row.

How to un-highline WPF DataGrid row?


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜