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基础-->流程控制-->分支结构-->单项分支-->双向分支
    python 控制语句基础---->代码块:以为冒号作为开始,用缩进来划分作用域,代表一个整体,是一个代码块,一个文件(模块)也称为一个代码块 | 作用域:作用的范围
    python基础语法
    Python Built-in Function 学习笔记
    身份证运算符 is 和 is not(检查两个数据在内存当中是否是同一个值) | 逻辑运算符 and or not | 数据类型的判断 isinstance
    算数运算符: +
    变量存储缓存机制 Number (int bool float complex)
    关于容器类型数据的强转一共:str() list() set() tuple() dict() 都可以转换成对应的数据类型 /Number 数据类型的强转一共: int() bool() flaot() complex() 都可以转换成对应的数据类型
    python 容器类型数据 (str list tuple set dict)
    Number 强制类型转换 int 强制转换整型 float 强制转换浮点型 complex 强制转换成复数 bool 强制转换成布尔类型,结果只有两种,要么True 要么 False """bool 可以转换所有的数据类型 everything"""
  • 原文地址:https://www.cnblogs.com/lipf/p/2473540.html
Copyright © 2011-2022 走看看