WPF中DataGrid的问题:1、根据row和col滚动至对应的DataGridCell并选择;2、选中某单元格如何得到其row和col,在线急求。。。

林夕合鸟 2014-12-01 01:47:07
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

请问诸位大大,如何破?
感谢感谢!!!!
...全文
417 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
林夕合鸟 2015-05-08
  • 打赏
  • 举报
回复
好久没来,将自己实现的代码发上来吧。顺带结贴,谢谢大家了。
dataGrid.ScrollIntoView(Data_Y_List[iRow]);
                dataGrid.UpdateLayout();
                DataGridCell dgr = PublicFunction.GetCell(dataGrid, iRow, iCol);
                if (dgr != null)
                {
                    dataGrid.SelectedItem = null;
                    dataGrid.UpdateLayout();
                    dgr.Focus();
                    dgr.IsSelected = true;
                }
黑娃 2014-12-02
  • 打赏
  • 举报
回复
怎么看着这么复杂呢 要滚动到指定的col和row你,直接调用datagrid的ScrollIntoView不就完了,知道了rowIndex就可知道它的row对象
  • 打赏
  • 举报
回复
引用 1 楼 kuainq 的回复:
没有滚动的话,是没有问题的!滚动就会出现以上的问题。这是什么么原因造成的,如何解决?谢谢了!
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 传入你的父控件类型。
林夕合鸟 2014-12-02
  • 打赏
  • 举报
回复
引用 8 楼 duanzi_peng 的回复:
[quote=引用 1 楼 kuainq 的回复:] 没有滚动的话,是没有问题的!滚动就会出现以上的问题。这是什么么原因造成的,如何解决?谢谢了!
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 传入你的父控件类型。[/quote] 谢谢大神,我来试试。!!!!
林夕合鸟 2014-12-02
  • 打赏
  • 举报
回复
引用 9 楼 falcomavin 的回复:
怎么看着这么复杂呢 要滚动到指定的col和row你,直接调用datagrid的ScrollIntoView不就完了,知道了rowIndex就可知道它的row对象
我选择的是Cell,你看上面的代码就能看到,我也写了ScrollIntoView,没有效果的。谢谢参与。
林夕合鸟 2014-12-01
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
http://download.csdn.net/download/duanzi_peng/8126501 下载DataGrid操作类。
这个对于我的需求都没有用啊。大神你再看看!!!
林夕合鸟 2014-12-01
  • 打赏
  • 举报
回复
你的需求都很蛋疼!选中的不刷新,其他的刷新[/quote] 没有办法啊! 需要修改这个数据,但是刷新会改变这个数据。应该也是比较常见的吧!?
灬浪子灬 2014-12-01
  • 打赏
  • 举报
回复
引用 3 楼 kuainq 的回复:
第二个问题: 如何得到当前DataGrid选中单元格的row和col? 因为我一直需要刷新数据,如果同时修改,那么输入党员框中的值,很可能被原来的数据(刷新)重新覆盖。 此时,我的计划是这样: 希望得到当前选择的单元格的row和col,根据他们确定对应的数据。软后在刷新数据的函数中,暂停对这个数据的更新,如果用户放弃修改,在将刷新结果付给该数据。 但是:我得不到row和col。请问如何得到? 或者是否有其他解决方案? 谢谢!
你的需求都很蛋疼!选中的不刷新,其他的刷新
  • 打赏
  • 举报
回复
林夕合鸟 2014-12-01
  • 打赏
  • 举报
回复
第二个问题: 如何得到当前DataGrid选中单元格的row和col? 因为我一直需要刷新数据,如果同时修改,那么输入党员框中的值,很可能被原来的数据(刷新)重新覆盖。 此时,我的计划是这样: 希望得到当前选择的单元格的row和col,根据他们确定对应的数据。软后在刷新数据的函数中,暂停对这个数据的更新,如果用户放弃修改,在将刷新结果付给该数据。 但是:我得不到row和col。请问如何得到? 或者是否有其他解决方案? 谢谢!
灬浪子灬 2014-12-01
  • 打赏
  • 举报
回复
引用 1 楼 kuainq 的回复:
没有滚动的话,是没有问题的!滚动就会出现以上的问题。这是什么么原因造成的,如何解决?谢谢了!
控制滚动条貌似不能实现
林夕合鸟 2014-12-01
  • 打赏
  • 举报
回复
没有滚动的话,是没有问题的!滚动就会出现以上的问题。这是什么么原因造成的,如何解决?谢谢了!

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧