C# wpf datagrid将重点放在selectemItem上

C# wpf datagrid将重点放在selectemItem上,c#,wpf,datagrid,focus,C#,Wpf,Datagrid,Focus,我以编程方式在datagrid中选择Item。 问题是我必须手动向下滚动到selectItem。我需要自动完成这项工作。 到目前为止,我已经尝试了很多东西,但没有任何东西对我有效 数据网格: <DataGrid x:Name="coreServiceLogDataGrid" ItemsSource="{Binding}" IsReadOnly="True" RowDetailsVisibil

我以编程方式在datagrid中选择Item。 问题是我必须手动向下滚动到selectItem。我需要自动完成这项工作。 到目前为止,我已经尝试了很多东西,但没有任何东西对我有效

数据网格:

        <DataGrid x:Name="coreServiceLogDataGrid"
              ItemsSource="{Binding}"
              IsReadOnly="True"
              RowDetailsVisibilityMode="VisibleWhenSelected"
              SelectionMode="Single"
              IsSynchronizedWithCurrentItem="True"
              SelectedItem="{Binding Path=CurrentCoreServiceLogDataItem,Source={StaticResource synchronizer}, Mode=TwoWay}"
              GotFocus="coreServiceLogDataGrid_GotFocus_1"
              Style="{DynamicResource ResourceKey=dataGridStyle}"
              ...>
              ...
    </DataGrid>

我过去也遇到过类似的问题。我已经为
DataGridRow
做了这项工作,但它是基于在上面找到的
TreeViewItem
的附加行为

以下是
代码隐藏的代码

/// <summary>
/// Exposes attached behaviors that can be
/// applied to DataGridRow objects.
/// </summary>
public static class DataGridRowBehavior
{
    #region IsBroughtIntoViewWhenSelected

    public static bool GetIsBroughtIntoViewWhenSelected(DataGridRow dataGridRow)
    {
        return (bool)dataGridRow.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetIsBroughtIntoViewWhenSelected(
      DataGridRow dataGridRow, bool value)
    {
        dataGridRow.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "IsBroughtIntoViewWhenSelected",
        typeof(bool),
        typeof(DataGridRowBehavior),
        new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    static void OnIsBroughtIntoViewWhenSelectedChanged(
      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)
    {
        // Only react to the Selected event raised by the TreeViewItem
        // whose IsSelected property was modified. Ignore all ancestors
        // who are merely reporting that a descendant's Selected fired.
        if (!Object.ReferenceEquals(sender, e.OriginalSource))
            return;

        DataGridRow item = e.OriginalSource as DataGridRow;
        if (item != null)
            item.BringIntoView();
    }

    #endregion // IsBroughtIntoViewWhenSelected
}

附加注释:我将
SelectionUnit
设置为
FullRow
并将
SelectionMode
设置为
Single
。我不确定更改这些属性是否会影响此操作是否有效。

您是否已验证事件
GotFocus
是否实际触发并且
coreServiceLogDataGrid。SelectedItem
不为空?@AbZy我已更新代码,但它仍然不适用于我。
/// <summary>
/// Exposes attached behaviors that can be
/// applied to DataGridRow objects.
/// </summary>
public static class DataGridRowBehavior
{
    #region IsBroughtIntoViewWhenSelected

    public static bool GetIsBroughtIntoViewWhenSelected(DataGridRow dataGridRow)
    {
        return (bool)dataGridRow.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetIsBroughtIntoViewWhenSelected(
      DataGridRow dataGridRow, bool value)
    {
        dataGridRow.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "IsBroughtIntoViewWhenSelected",
        typeof(bool),
        typeof(DataGridRowBehavior),
        new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    static void OnIsBroughtIntoViewWhenSelectedChanged(
      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)
    {
        // Only react to the Selected event raised by the TreeViewItem
        // whose IsSelected property was modified. Ignore all ancestors
        // who are merely reporting that a descendant's Selected fired.
        if (!Object.ReferenceEquals(sender, e.OriginalSource))
            return;

        DataGridRow item = e.OriginalSource as DataGridRow;
        if (item != null)
            item.BringIntoView();
    }

    #endregion // IsBroughtIntoViewWhenSelected
}
<DataGrid.ItemContainerStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="uc:DataGridRowBehavior.IsBroughtIntoViewWhenSelected" Value="True" />
    </Style>
</DataGrid.ItemContainerStyle>
/// note: uc is a namespace I have defined for where the DataGridRowBehavior class is located