多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術 > WPF中TreeView.BringIntoView方法的替代方案

WPF中TreeView.BringIntoView方法的替代方案

來源:程序員人生   發(fā)布時間:2014-03-13 01:55:09 閱讀次數(shù):3137次

  建站學院(LieHuo.Net)編程綜合 WPF中TreeView.BringIntoView()方法并不是那么地好用,不少時候會沒有效果,這里有一個替代方案,調用SelectItem()方法可以展開并呈現(xiàn)TreeView上指定的Item:

以下為引用的內容:
 public static class TreeViewHelper

    {

        /// <summary>

        /// Expands all children of a TreeView

        /// </summary>

        /// <param name="treeView">The TreeView whose children will be expanded</param>

        public static void ExpandAll(this TreeView treeView)

        {

            ExpandSubContainers(treeView);

        }

        /// <summary>

        /// Expands all children of a TreeView or TreeViewItem

        /// </summary>

        /// <param name="parentContainer">The TreeView or TreeViewItem containing the children to expand</param>

        private static void ExpandSubContainers(ItemsControl parentContainer)

        {

            foreach (Object item in parentContainer.Items)

            {

                TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                if (currentContainer != null && currentContainer.Items.Count > 0)

                {

                    //expand the item

                    currentContainer.IsExpanded = true;

                    //if the item's children are not generated, they must be expanded

                    if (currentContainer.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)

                    {

                        //store the event handler in a variable so we can remove it (in the handler itself)

                        EventHandler eh = null;

                        eh = new EventHandler(delegate

                            {

                                //once the children have been generated, expand those children's children then remove the event handler

                                if (currentContainer.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)

                                {

                                    ExpandSubContainers(currentContainer);

                                    currentContainer.ItemContainerGenerator.StatusChanged -= eh;

                                }

                            });

                        currentContainer.ItemContainerGenerator.StatusChanged += eh;

                    }

                    else //otherwise the children have already been generated, so we can now expand those children

                    {

                        ExpandSubContainers(currentContainer);

                    }

                }

            }

        }

        /// <summary>

        /// Searches a TreeView for the provided object and selects it if found

        /// </summary>

        /// <param name="treeView">The TreeView containing the item</param>

        /// <param name="item">The item to search and select</param>

        public static void SelectItem(this TreeView treeView, object item)

        {

            ExpandAndSelectItem(treeView, item);

        }

        /// <summary>

        /// Finds the provided object in an ItemsControl's children and selects it

        /// </summary>

        /// <param name="parentContainer">The parent container whose children will be searched for the selected item</param>

        /// <param name="itemToSelect">The item to select</param>

        /// <returns>True if the item is found and selected, false otherwise</returns>

        private static bool ExpandAndSelectItem(ItemsControl parentContainer, object itemToSelect)

        {

            //check all items at the current level

            foreach (Object item in parentContainer.Items)

            {

                TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                //if the data item matches the item we want to select, set the corresponding

                //TreeViewItem IsSelected to true

                if (item == itemToSelect && currentContainer != null)

                {

                    currentContainer.IsSelected = true;

                    currentContainer.BringIntoView();

                    currentContainer.Focus();

                    //the item was found

                    return true;

                }

            }

            //if we get to this point, the selected item was not found at the current level, so we must check the children

            foreach (Object item in parentContainer.Items)

            {

                TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                //if children exist

                if (currentContainer != null && currentContainer.Items.Count > 0)

                {

                    //keep track of if the TreeViewItem was expanded or not

                    bool wasExpanded = currentContainer.IsExpanded;

                    //expand the current TreeViewItem so we can check its child TreeViewItems

                    currentContainer.IsExpanded = true;

                    //if the TreeViewItem child containers have not been generated, we must listen to

                    //the StatusChanged event until they are

                    if (currentContainer.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)

                    {

                        //store the event handler in a variable so we can remove it (in the handler itself)

                        EventHandler eh = null;

                        eh = new EventHandler(delegate

                        {

                            if (currentContainer.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)

                            {

                                if (ExpandAndSelectItem(currentContainer, itemToSelect) == false)

                                {

                                    //The assumption is that code executing in this EventHandler is the result of the parent not

                                    //being expanded since the containers were not generated.

                                    //since the itemToSelect was not found in the children, collapse the parent since it was previously collapsed

                                    currentContainer.IsExpanded = false;

                                }

                                //remove the StatusChanged event handler since we just handled it (we only needed it once)

                                currentContainer.ItemContainerGenerator.StatusChanged -= eh;

                            }

                        });

                        currentContainer.ItemContainerGenerator.StatusChanged += eh;

                    }

                    else //otherwise the containers have been generated, so look for item to select in the children

                    {

                        if (ExpandAndSelectItem(currentContainer, itemToSelect) == false)

                        {

                            //restore the current TreeViewItem's expanded state

                            currentContainer.IsExpanded = wasExpanded;

                        }

                        else //otherwise the node was found and selected, so return true

                        {

                            return true;

                        }

                    }

                }

            }

            //no item was found

            return false;

        }

    }

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 最近最新的免费中文字幕 | 欧美性猛交xxxx黑人喷水 | 亚洲成人免费在线观看 | 久久不卡免费视频 | 亚洲人成伊人成综合网久久久 | 91久久精品都在这里 | 福利在线看片 | 日韩1区| 亚洲hd| 亚洲毛片免费视频 | 亚洲第一免费 | 可以免费看黄色的网站 | 手机看片亚洲 | 香蕉tv亚洲专区在线观看 | 中文字幕日韩欧美一区二区三区 | 日本xxxwww色视频 | 亚洲线精品久久一区二区三区 | 国产人成亚洲第一网站在线播放 | 国产99精品一区二区三区免费 | 精品国产综合区久久久久久 | 欧美三级在线观看视频 | 伊人影视| 日韩欧美视频 | 欧美精品高清 | 性欧美另类老妇高清 | 免费观看无遮挡www的小视频 | 国产一区二区精品久久 | 在线日本中文字幕 | 天堂日韩| 久久综合九色综合亚洲 | 伊人久久大香 | 亚洲伦乱| 欧美日韩成人高清在线播放 | 欧美国产日韩精品 | 国产v片成人影院在线观看 国产v片在线观看 | 一级a毛片免费 | 性xx视频| 美国美女一级毛片免费全 | 久草在线观看首页 | 欧美三级午夜理伦三级小说 | 高清在线精品一区二区 |