【问题标题】:Set the focus to a specific datagrid cell将焦点设置到特定的数据网格单元格
【发布时间】:2016-06-30 16:48:06
【问题描述】:

我正在尝试根据输入的零件编号设置数据网格中单元格的焦点,以便用户可以对零件进行条形码扫描,然后快速编辑该值。现在,我可以在我拥有的项目的 ObservableCollection 中找到部件号的索引,但我在尝试将其设置为该网格单元时遇到了麻烦。 grTimeEntries 是数据网格的名称。下面的当前代码在尝试创建新的 DataGridCellInfo 时给了我一个错误

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

代码:

    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();

            }
        }
    }

【问题讨论】:

    标签: c# wpf datagrid focus cell


    【解决方案1】:

    创建一个依赖属性来聚焦单元格对你有用。

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

    如下设置您的 XAML,

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

    绑定属性

    public Object[] Cell
    {
       get;
       set;
    } 
    

    【讨论】:

      猜你喜欢
      • 2011-03-26
      • 1970-01-01
      • 2015-05-22
      • 2011-01-29
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      相关资源
      最近更新 更多