如何在wpf gatagrid中设置当前单元格 [英] how to set current cell in wpf gatagrid

查看:71
本文介绍了如何在wpf gatagrid中设置当前单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai



我有一个名为Dg的Datagrid,我想以编程方式更改行和列。



我使用此代码



Dg.CurrentCell =新DataGridCellInfo(Dg.Items(IntRow),Dg.Columns(IntCol))

用于设置当前单元格,这样可以正常工作,但所选单元格的蓝色位于同一单元格中。请告诉我如何将这个焦点与当前单元格一起移动。



我不要制作Dg.SelectedCells.Clear(),因为这样只删除选择颜色和没关注。





谢谢

解决方案

您好使用下面的代码,



 DataGridCell objCellToFocus = DataGridExtensions.GetCell(Dg,(DataGridRow)Dg.ItemContainerGenerator.ContainerFromIndex( 1 ), 2 ); 
objCellToFocus.Focus();



  public   static   class  DataGridExtensions 
{
public static DataGridCell GetCell( DataGrid网格,DataGridRow行, int columnIndex = 0
{
if ( row == null return null ;

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

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

// 现在尝试进入查看并检索单元格
grid.ScrollIntoView(row,grid.Columns [columnIndex]);
cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);

return cell;
}

public static IEnumerable< t> FindVisualChildren< t>(DependencyObject depObj)其中 T:DependencyObject
{
if (depObj!= null
{
for int i = 0 ; i < VisualTreeHelper.GetChildrenCount(depObj ); i ++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
if (child!= null && child T)
{
yield return ( T)的孩子;
}

foreach (T childOfChild FindVisualChildren< t> (孩子))
{
yield return childOfChild;
}
}
}
}

public static childItem FindVisualChild< childitem>(DependencyObject obj)其中​​ childItem:DependencyObject
{
foreach (childItem child in FindVisualChildren< childitem>(obj))
{
返回孩子;
}

return null ;
}
}
< / childitem > < / childitem > < / t > < / t > < / t > < / datagridcellspresenter >


Hai

I have a Datagrid called Dg and I want to programatically change row and column.

I use this code

Dg.CurrentCell = New DataGridCellInfo(Dg.Items(IntRow), Dg.Columns(IntCol))
for setting the current cell and this is working fine but the blue color of selected cell stands in the same cell. Please tell me how to move this focus along with the current cell.

I don't to make Dg.SelectedCells.Clear() as this removes only selection color and not focus.


Thanks

解决方案

Hi use the following code,

DataGridCell objCellToFocus = DataGridExtensions.GetCell(Dg,(DataGridRow)Dg.ItemContainerGenerator.ContainerFromIndex(1), 2);
            objCellToFocus.Focus();


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

            var presenter = FindVisualChild<datagridcellspresenter>(row);
            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>(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>(DependencyObject obj) where childItem : DependencyObject
        {
            foreach (childItem child in FindVisualChildren<childitem>(obj))
            {
                return child;
            }

            return null;
        }
    }
</childitem></childitem></t></t></t></datagridcellspresenter>


这篇关于如何在wpf gatagrid中设置当前单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆