WPF中DataGrid的有关问题:1、根据row和col滚动至对应的DataGridCell并选择;2、选中某单元格怎么得到其row和col,在线急求。
WPF中DataGrid的问题:1、根据row和col滚动至对应的DataGridCell并选择;2、选中某单元格如何得到其row和col,在线急求。。。
WPF中DataGrid的问题:
1、根据row和col滚动至对应的DataGridCell并选择;
会有滚动条出现的情况。
以下是我现在的代码:
如果需要滚动:在GetVisualChild函数的GetChildrenCount得到的是0
请问诸位大大,如何破?
感谢感谢!!!!
------解决思路----------------------
http://download.****.net/download/duanzi_peng/8126501
下载DataGrid操作类。
------解决思路----------------------
你的需求都很蛋疼!选中的不刷新,其他的刷新
------解决思路----------------------
parent 类型不应该是 Visual的吧,是DependencyObject类型的吧?
用这个方法测试一下。
obj 传入你的父控件类型。
------解决思路----------------------
怎么看着这么复杂呢
要滚动到指定的col和row你,直接调用datagrid的ScrollIntoView不就完了,知道了rowIndex就可知道它的row对象
WPF中DataGrid的问题:
1、根据row和col滚动至对应的DataGridCell并选择;
会有滚动条出现的情况。
以下是我现在的代码:
public DataGridCell GetCell(DataGrid dataGrid, int rowIndex, int columnIndex)
{
DataGridRow rowContainer = GetRow(dataGrid,rowIndex);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter != null)
{
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
if (cell == null)
{
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return cell;
}
}
return null;
}
public DataGridRow GetRow(DataGrid dataGrid, int rowIndex)
{
DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
if (rowContainer == null)
{
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
}
return rowContainer;
}
public T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T; if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
如果需要滚动:在GetVisualChild函数的GetChildrenCount得到的是0
请问诸位大大,如何破?
感谢感谢!!!!
------解决思路----------------------
http://download.****.net/download/duanzi_peng/8126501
下载DataGrid操作类。
------解决思路----------------------
你的需求都很蛋疼!选中的不刷新,其他的刷新
------解决思路----------------------
parent 类型不应该是 Visual的吧,是DependencyObject类型的吧?
用这个方法测试一下。
public T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement
{
DependencyObject child = null;
T grandChild = null;
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name == name
------解决思路----------------------
string.IsNullOrEmpty(name)))
{
return (T)child;
}
else
{
grandChild = GetChildObject<T>(child, name);
if (grandChild != null)
return grandChild;
}
}
return null;
}
obj 传入你的父控件类型。
------解决思路----------------------
怎么看着这么复杂呢
要滚动到指定的col和row你,直接调用datagrid的ScrollIntoView不就完了,知道了rowIndex就可知道它的row对象