日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

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

來源:程序員人生   發布時間:2014-03-13 01:55:09 閱讀次數:3143次

  建站學院(LieHuo.Net)編程綜合 WPF中TreeView.BringIntoView()方法并不是那么地好用,不少時候會沒有效果,這里有一個替代方案,調用SelectItem()方法可以展開并呈現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;

        }

    }

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 成人黄色免费网站 | 亚洲精彩免费视频 | 亚洲精品福利电影 | 国产欧美综合一区二区三区 | 国产高清av在线 | 国产成人久久精品 | 二区精品| 国产精品国产三级国产专播品爱网 | 亚洲二区视频 | 中文av字幕在线观看 | 91精品国产91久久久久久吃药 | 视频精品一区 | 国产精品999| www.四虎.com | aⅴ免费在线观看 | 黄色网址在线免费观看 | 成人欧美一区二区三区视频xxx | 在线观看国产麻豆 | 成人久久久久久久 | 亚洲综合字幕 | 国产呦精品一区二区三区网站 | 久久久久99精品国产片 | 国产99视频精品免费视频7 | 国产精品视频一区二区免费不卡 | 精品三级在线观看 | 国产精品亚洲成人 | 日韩有码一区二区三区 | 国产成人高清视频 | 爱爱视频网站 | 九九精品视频在线观看 | jlzzjlzz亚洲女人18 | 国产成人精品一区二区三区在线 | 成人黄色av网站 | 免费在线播放av | 99re国产精品 | 欧美日韩中文字幕在线视频 | 日日噜噜夜夜狠狠 | 亚洲精品日韩av | 精品国产乱码久久久久久88av | 国产精品久久久久久久久久久久 | 99福利在线观看 |