简体   繁体   中英

Highlighting the selected Item in WPF Datagrid

my question seems to be an easy task but somehow all the things i tried won't give me the desired result.

In my MainWindow i have a ContentControl that binds to a variable called "CurrentView", wich is a ViewModel. I switch the CurrentView via a Navigationbar. In my first View there is a DataGrid. When i click on an element on that DataGrid the Row gets highlighted in Blue and the selected Item gets saved in my ViewModel.

When i now jump to a new View, and back to the first View the selected Item is still selected in the DataGrid, but the Row is not highlighted…

I tried so many Things but somehow i cant get the Row Highlighted.

Is there something im missing?

You need to focus the DataGrid for the selected row to become highlighted by default. You could for example call the Focus() method of the DataGrid in a Loaded event handler for the view that you are navigating to:

public partial class View1 : UserControl
{
    public View1()
    {
        InitializeComponent();
        Loaded += (s, e) => dataGrid1.Focus();
    }
}

AFAIK you need focus on the specific row. I use this behaviour:

using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interactivity;

namespace blaa
{
class DataGridRowBehavior : Behavior<DataGridRow>
{
    public static bool GetIsDataGridRowFocussedWhenSelected(DataGridRow dataGridRow)
    {
        return (bool)dataGridRow.GetValue(IsDataGridRowFocussedWhenSelectedProperty);
    }

    public static void SetIsDataGridRowFocussedWhenSelected(
      DataGridRow dataGridRow, bool value)
    {
        dataGridRow.SetValue(IsDataGridRowFocussedWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsDataGridRowFocussedWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "IsDataGridRowFocussedWhenSelected",
        typeof(bool),
        typeof(DataGridRowBehavior),
        new UIPropertyMetadata(false, OnIsDataGridRowFocussedWhenSelectedChanged));

    static void OnIsDataGridRowFocussedWhenSelectedChanged(
      DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        DataGridRow item = depObj as DataGridRow;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
            item.Selected += OndataGridRowSelected;
        else
            item.Selected -= OndataGridRowSelected;
    }
    static void OndataGridRowSelected(object sender, RoutedEventArgs e)
    {
        DataGridRow row = e.OriginalSource as DataGridRow;
        // If focus is already on a cell then don't focus back out of it
        if (!(Keyboard.FocusedElement is DataGridCell) && row != null)
        {
            row.Focusable = true;
            Keyboard.Focus(row);
        }
    }

  }
}

Usage:

        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="local:DataGridRowBehavior.IsDataGridRowFocussedWhenSelected" Value="true"/>
            </Style>
        </DataGrid.RowStyle>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM