2013-02-21 2 views
0

Я программно выбираюItem в datagrid. Проблема в том, что мне нужно вручную прокрутить вниз до selectItem. Мне нужно сделать это автоматически. до сих пор я пытался много, что и ничего не работает для меня ...wpf datagrid focus on the selectemItem

DataGrid:

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

и код позади для GotFocus:

 private void coreServiceLogDataGrid_GotFocus_1(object sender, System.Windows.RoutedEventArgs e) { 
      if (coreServiceLogDataGrid.SelectedItem != null) { 
      coreServiceLogDataGrid.ScrollIntoView(coreServiceLogDataGrid.SelectedItem); 
      } 
     } 
+0

Вы подтвердили, что событие 'GotFocus' на самом деле пожары и' coreServiceLogDataGrid.SelectedItem' не равно нулю? – AbZy

+0

@AbZy У меня есть обновленный код, но он по-прежнему не работает для меня. – RayOldProf

ответ

0

Я столкнулся с подобным вопросом в прошлом. Я сделал это для DataGridRow, но он основан на приложенном поведении для TreeViewItem, найденного на этом site.

Вот код code-behind:

/// <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 
} 

и в вашем XAML, поместите этот код между тегами для DataGrid:

<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 

Дополнительного комментария: У меня есть SelectionUnit набора для FullRow и SelectionMode установлены на Single. Я не уверен, что изменение этих свойств повлияет на то, будет ли это работать.