WPF 中获取datagrid中的值

来源:互联网 时间:1970-01-01

       public static void SetDataGridCellIsEnabled(DataGrid datagrid, int rowIndex, int[] cellIndexArray, bool IsEnabled)

        {            for (int index = 0; index < cellIndexArray.Length; index++)
            {
                DataGridCell currentCell = GetDataGridCell(datagrid, rowIndex, cellIndexArray[index]);
                if (currentCell != null)
                {
                    currentCell.IsEnabled = IsEnabled; 

                    这行代码是隐藏单元格




--------------------------------------------------------------------------------


                    Button button1= currentCell.ChildrenOfType<Button>(); 

                     这行代码是得到模板控件里的子控件 
                }
            }
        }

        public static DataGridCell GetDataGridCell(DataGrid datagrid, int rowIndex, int columnIndex)
        {
            try
            {
                DataGridRow rowContainer = GetDataGridRow(datagrid, rowIndex);
                
                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 

                    这行代码是通过行得到单元格


--------------------------------------------------------------------------------

                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); 

                    这行代码是通过index得到具体的单元格 



--------------------------------------------------------------------------------

                    if (cell == null)
                    {
                        datagrid.ScrollIntoView(rowContainer, datagrid.Columns[columnIndex]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    }

                    return cell;
                }
            }
            catch
            {
                return null;
            }
            return new DataGridCell();
        }

        public static DataGridRow GetDataGridRow(DataGrid datagrid, int rowIndex)
        {
            DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(rowIndex); 

            这行代码的作用是得到datagrid的一行 


--------------------------------------------------------------------------------


            if (row == null)
            {
                datagrid.UpdateLayout();
                //datagrid.ScrollIntoView(datagrid.Items[rowIndex]);
                row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
            }
            return row;
        }

        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T childContent = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                childContent = v as T;
                if (childContent == null)
                {
                    childContent = GetVisualChild<T>(v);
                }
                if (childContent != null)
                {
                    break;
                }
            }
            return childContent;
        }

本文来自xiaozhiboxiaozhibo的博客,原文地址:http://blog.csdn.net/xiaozhiboxiaozhibo/archive/2010/04/26/5529603.aspx


相关阅读:
Top