如何在WPF 4.0数据网格中获取Currentcell的rowindex [英] how to get rowindex of Currentcell in WPF 4.0 datagrid

查看:220
本文介绍了如何在WPF 4.0数据网格中获取Currentcell的rowindex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我发现很难在WPF 4.0中获取数据网格的当前选定单元格的行索引?我根本不需要当前单元格的任何值或项目.

如果有任何明确的想法,请回复.
谢谢

我真的很困惑,WPF相对于其兄弟的Windows窗体控件而言,在数据处理方面真的很丰富吗?
在WPF datagrid中找到简单的属性/功能似乎很头疼,这些属性/功能由datagridview以Windows形式提供.

似乎正在建立更好的控件(不是按外观)
从Windows窗体迁移到WPF时,编码方式更改了70%.:confused:

Hi guys,

I am finding it quite difficult to get a rowindex of a current selected cell of a datagrid in WPF 4.0? I don''t want at all value or item of current cell.

If any body has clear idea please do reply.
Thanks

I am really confused,Is really WPF rich in terms of data handling with respect to its brother hood windows form''s controls?
It seems to be headache for finding simple properties/facility in WPF datagrid which are provided by datagridview in windows form.

Seems to be in building process of better controls(not by looks)
The way of coding is changed 70 percent while migrating to WPF from windows forms.:confused:

推荐答案

尝试

//全局声明委托
公共委托Point GetDragDropPosition(IInputElement theElement);
int prevRowIndex = -1;

我们可以从datagrid预览鼠标左键按下事件中获取它.
在构造函数级别进行声明

公共MainWindow()
{
InitializeComponent();
this.dgEmployee.PreviewMouseLeftButtonDown + =新的MouseButtonEventHandler(dgEmployee_PreviewMouseLeftButtonDown);
}



私有无效dgEmployee_PreviewMouseLeftButtonDown(对象发送者,MouseButtonEventArgs e)
{
prevRowIndex = GetDataGridItemCurrentRowIndex(e.GetPosition);

如果(prevRowIndex< 0)
返回;

dgEmployee.SelectedIndex = prevRowIndex;

}

获取当前行索引的方法
私有int GetDataGridItemCurrentRowIndex(GetDragDropPosition pos)
{
int currIndex = -1;
for(int i = 0; i< dgEmployee.Items.Count; i ++)
{
DataGridRow项= GetDataGridRowItem(i);
if(IsTheMouseOnTargetRow(item,pos))
{
currIndex = i;
休息;
}
}
return currIndex;
}


只是尝试一下.. !!
私人布尔IsTheMouseOnTargetRow(Visual theTarget,GetDragDropPosition pos)
{
矩形posBounds = VisualTreeHelper.GetDescendantBounds(theTarget);
点theMousePos = pos((IInputElement)theTarget);
返回posBounds.Contains(theMousePos);
}

私有DataGridRow GetDataGridRowItem(int索引)
{
如果(dgEmployee.ItemContainerGenerator.Status!= GeneratorStatus.ContainersGenerated)
返回null;
将dgEmployee.ItemContainerGenerator.ContainerFromIndex(index)返回为DataGridRow;
}
// Declare the delegate globally
public delegate Point GetDragDropPosition(IInputElement theElement);
int prevRowIndex = -1;

We can get it from datagrid preview mouse left button down event.
Declare it at the constructor level

public MainWindow()
{
InitializeComponent();
this.dgEmployee.PreviewMouseLeftButtonDown+=new MouseButtonEventHandler(dgEmployee_PreviewMouseLeftButtonDown);
}



private void dgEmployee_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
prevRowIndex = GetDataGridItemCurrentRowIndex(e.GetPosition);

if (prevRowIndex < 0)
return;

dgEmployee.SelectedIndex = prevRowIndex;

}

Method to get the current row index
private int GetDataGridItemCurrentRowIndex(GetDragDropPosition pos)
{
int currIndex = -1;
for (int i = 0; i < dgEmployee.Items.Count; i++)
{
DataGridRow item = GetDataGridRowItem(i);
if(IsTheMouseOnTargetRow(item,pos))
{
currIndex=i;
break;
}
}
return currIndex;
}


Just try this out..!!
private bool IsTheMouseOnTargetRow(Visual theTarget, GetDragDropPosition pos)
{
Rect posBounds = VisualTreeHelper.GetDescendantBounds(theTarget);
Point theMousePos = pos((IInputElement)theTarget);
return posBounds.Contains(theMousePos);
}

private DataGridRow GetDataGridRowItem(int index)
{
if (dgEmployee.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
return null;
return dgEmployee.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
}


这篇关于如何在WPF 4.0数据网格中获取Currentcell的rowindex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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