zoukankan      html  css  js  c++  java
  • 获取WPF树结构中的控件

    public class DataGridHelper
        {
            private DataGrid dataGrid;
            public DataGridHelper(DataGrid dataGrid)
            {
                this.dataGrid = dataGrid;
            }

            public T FindCellControl<T>(string name, int columnIndex) where T : Visual
            {
                DataRowView selectItem = dataGrid.SelectedItem as DataRowView;
                DataGridCell cell = GetCell(dataGrid, dataGrid.SelectedIndex, columnIndex);

                return FindVisualChildByName<T>(cell, name) as T;
            }

            public T FindVisualChildByName<T>(Visual parent, string name) where T : Visual
            {
                if (parent != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                    {
                        var child = VisualTreeHelper.GetChild(parent, i) as Visual;
                        string controlName = child.GetValue(Control.NameProperty) as string;
                        if (controlName == name)
                        {
                            return child as T;
                        }
                        else
                        {
                            T result = FindVisualChildByName<T>(child, name);
                            if (result != null)
                                return result;
                        }
                    }
                }
                return null;
            }

            public static DataGridCell GetCell(DataGrid datagrid, int rowIndex, int columnIndex)
            {
                DataGridRow rowContainer = GetRow(datagrid, rowIndex);

                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

                    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 static DataGridRow GetRow(DataGrid datagrid, int columnIndex)
            {
                DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                if (row == null)
                {
                    datagrid.UpdateLayout();
                    datagrid.ScrollIntoView(datagrid.Items[columnIndex]);
                    row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                }
                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;
            }


        }

  • 相关阅读:
    python 全栈开发,Day67(Django简介)
    python 全栈开发,Day66(web应用,http协议简介,web框架)
    python 全栈开发,Day65(MySQL练习题,参考答案)
    python 全栈开发,Day65(索引)
    python 全栈开发,Day64(视图,触发器,函数,存储过程,事务)
    python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)
    *** 安全沙箱冲突 *** 到 127.0.0.1:9999 的连接已停止
    PosPal银豹收银系统
    m3u8文件简介
    flashbuilder发布release版本
  • 原文地址:https://www.cnblogs.com/lipf/p/2473540.html
Copyright © 2011-2022 走看看