2016-06-30 5 views
0

입력 된 부품 번호를 기반으로하는 DataGrid에서 셀의 포커스를 설정하려고합니다. 사용자가 바코드로 부품을 스캔 한 다음 값을 매우 빠르게 편집 할 수 있습니다. 이제 ObservableCollection에서 부품 번호의 색인을 찾을 수 있지만 해당 표 셀에 설정하는 데 문제가 있습니다. grTimeEntries는 데이터 격자의 이름입니다. 당신을 위해 작동합니다 셀을 집중 종속성 속성을 만들기특정 DataGrid 셀에 포커스 설정

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     string ToFind = tbIPN.Text; 
     for (int i = 0; i < (grMain.DataContext as DatabaseViewmodel).items.Count; i++) 
     { 
      if ((grMain.DataContext as DatabaseViewmodel).items[i].IPN == ToFind) 
      { 
       grTimeEntries.CurrentCell = new DataGridCellInfo { grTimeEntries.Items[i], grTimeEntries.Columns[1] }; 
       //grTimeEntries.SelectedIndex = i;   Focussed entire row. Wont work if i have selectionunit = "Cell". 
       grTimeEntries.Focus(); 

      } 
     } 
    } 

답변

0

: 새로운 DataGridCellInfo

Error 1 Cannot initialize type 'System.Windows.Controls.DataGridCellInfo' with a collection initializer because it does not implement 'System.Collections.IEnumerable' 

코드를 생성 할 때 현재 아래 코드는 나에게 오류를 제공합니다.

public class EDataGridCellFocus 
{ 
    public static object GetFocusedCell(DependencyObject obj) 
    { 
     return obj.GetValue(IsFocusedProperty); 
    } 

    public static void SetFocusedCell(DependencyObject obj, object value) 
    { 
     obj.SetValue(IsFocusedProperty, value); 
    } 

    public static readonly DependencyProperty IsFocusedProperty = 
     DependencyProperty.RegisterAttached(
     "FocusedCell", typeof(object), typeof(EDataGridCellFocus), 
     new UIPropertyMetadata(false, null, OnCoerceValue)); 

    private static object OnCoerceValue(DependencyObject d, object baseValue) 
    { 
     if (((DataGrid)d).Items.Count > 0 || ((DataGrid)d).HasItems) 
     { 
      var row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(baseValue[0]) as DataGridRow; 
      if (row != null) 
      { 
       var cell = ((DataGrid)d).GetCell(row, baseValue[1]); 

        Keyboard.ClearFocus(); 
        FocusManager.SetIsFocusScope(d, true); 
        FocusManager.SetFocusedElement(cell, cell); 
        Keyboard.Focus(cell);      
      } 
     } 
     return baseValue; 
    } 
} 

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int columnIndex = 0) 
    { 
     if (row == null) return null; 

     var presenter = row.FindVisualChild<DataGridCellsPresenter>(); 
     if (presenter == null) return null; 

     var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); 
     if (cell != null) return cell; 

     // now try to bring into view and retreive the cell 
     grid.ScrollIntoView(row, grid.Columns[columnIndex]); 
     cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); 

     return cell; 
    } 
    public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) 
    where T : DependencyObject 
    { 
     if (depObj != null) 
     { 
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
      { 
       DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
       if (child != null && child is T) 
       { 
        yield return (T)child; 
       } 

       foreach (T childOfChild in FindVisualChildren<T>(child)) 
       { 
        yield return childOfChild; 
       } 
      } 
     } 
    } 

    public static childItem FindVisualChild<childItem>(this DependencyObject obj) 
     where childItem : DependencyObject 
    { 
     foreach (childItem child in FindVisualChildren<childItem>(obj)) 
     { 
      return child; 
     } 
     return null; 
    } 

<DataGrid EDataGridCellFocus.FocusedCell="{Binding Cell}"/> 

public Object[] Cell 
{ 
    get; 
    set; 
} 
재산권

바인딩, 아래처럼 XAML을 설정
관련 문제