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;
            }


        }

  • 相关阅读:
    ADB命令大全
    Backup your Android without root or custom recovery -- adb backup
    Content portal for Pocketables Tasker articles
    Is there a way to detect if call is in progress? Phone Event
    Tasker to proximity screen off
    Tasker to detect application running in background
    Tasker to create toggle widget for ES ftp service -- Send Intent
    Tasker to proximity screen on
    Tasker to answer incoming call by pressing power button
    Tasker to stop Poweramp control for the headset while there is an incoming SMS
  • 原文地址:https://www.cnblogs.com/lipf/p/2473540.html
Copyright © 2011-2022 走看看