2013-02-21 89 views
0

我以編程方式在數據網格中選擇項目。 問題是,雖然我不得不手動向下滾動selectItem。我需要自動執行此操作。 到目前爲止,我已經嘗試了許多的事情,沒有什麼工作對我來說...wpf datagrid專注於選擇項目

的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'不爲null? – AbZy 2013-02-21 19:26:23

+0

@AbZy我有更新的代碼,但它仍然不適合我。 – RayOldProf 2013-02-21 19:31:47

回答

0

我碰到了類似的問題以往。我已經爲DataGridRow完成了此操作,但它基於此site上發現的TreeViewItem的附加行爲。

下面是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設置爲FullRowSelectionMode設置爲Single。我不確定改變這些屬性是否會影響這是否可行。