none
关于DataGrid获取单元格的内容的问题 RRS feed

  • 问题

  • DataGridRow rowContainer = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(rowIndex);
    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
     
    // ...
     
    public static 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);
        else
          break;
      }
     
      return child;
    }
    利用这段代码中rowContainer为null,我定义的DataGrid里有1个DataGridTextColumn和7个DataGridTemplateColumn,绑定了2个数据,但是总是返回null,不知道为什么。真心求解。希望大家能帮帮我,非常感谢
    需求是我在一个DataGrid里面的某个位置想添加一个控件,我该怎么弄。
    比如一个8*8的Datagrid,我在坐标为(4,4)的位置添加一个Button





    • 已编辑 性手young 2013年12月2日 8:41 补充问题
    2013年12月2日 8:09

答案

  • 您好,

    >>获取DataGrid中内容,可以参考下面的代码:

    C# Code

    private void GetDataGridContent()
    {
    	DataGridCell cell = GetCell(4, 1);
    	TextBlock tb = cell.Content as TextBlock;
    	MessageBox.Show(tb.Text);
    	//....
    }
    
    public DataGridCell GetCell(int row, int column)
    {
    	DataGridRow rowContainer = GetRow(row);
    
    	if (rowContainer != null)
    	{
    		DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
    
    		DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
    		if (cell == null)
    		{
    			dataGrid1.ScrollIntoView(rowContainer, dataGrid1.Columns[column]);
    			cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
    		}
    		return cell;
    	}
    	return null;
    }
    
    public DataGridRow GetRow(int index)
    {
    	DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
    	if (row == null)
    	{
    		dataGrid1.UpdateLayout();
    		dataGrid1.ScrollIntoView(dataGrid1.Items[index]);
    		row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
    	}
    	return row;
    }
    
    public static 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;
    }

    >>DataGrid中指定位置添加Button

    我建议您这样来操作,先将这个Button放在指定位置,隐藏掉。当满足您的条件时,再将它显示。这样比较好做些。

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月3日 5:45
    版主