Blog Archive

Tuesday, September 23, 2014

Using DevExpress Layout Manager access a View from Shell

When View (UserControl) cannot be access from Presenter and burried inside inside a shared DLL, you may use Shell Window
 to walk up/down tree. But some docking situation requires using DockLayoutManager to track down. 


            var shellWindow = _mySvc.GetApplicationShell() as XpfRibbonShellView;
            if (shellWindow == null) return;

            shellWindow.Dispatcher.Invoke(new Action(() =>
            {
                try
                {
                    // when TheView is docked
                    var wrkSpace = VisualTreeHelpers.FindChild<ContentControl>(shellWindow, "wrkSpace");
                    var gContent = VisualTreeHelpers.FindChild<GroupPaneContentPresenter>(wrkSpace, "PART_Content");
                    var lpItemsCtl = VisualTreeHelpers.FindChild<LayoutItemsControl>(gContent);

                    foreach (var i in lpItemsCtl.Items)
                    {
                        // TheView docked top layer
                        var lp = i as LayoutPanel;
                        if (lp != null && lp.Content is TheView)
                            lp.ShowCaption = true;


                        // TheView Tabbed inside another docked
                        var lg = i as LayoutGroup;
                        if (lg == null) continue;
                        foreach (var i2 in lg.Items)
                        {
                            var tg = i2 as TabbedGroup;
                            if (tg != null)
                            {
                                var layoutItems = tg.GetItems();
                                foreach (var lp2 in layoutItems.Cast<LayoutPanel>().Where(lp2 => lp2.Content is TheView))
                                {
                                    lp2.ShowCaption = true;
                                }
                            }
                            // TheView could end up here if close and then added back whiel in Tab mode
                            var lp3 = i2 as LayoutPanel;
                            if (lp3 != null && lp3.Content is TheView)
                                lp3.ShowCaption = true;
                        }
                    }

                    // When TheView is floating
                    foreach (var lp in shellWindow.DockLayoutManager.FloatGroups.SelectMany(fg => fg.Items.OfType<LayoutPanel>().Where(lp => lp.Content is TheView)))
                    {
                        lp.ShowCaption = true;
                    }

                    // All auto hide layout panel need to be handled here , not just TheView.
                    foreach (var lp in shellWindow.DockLayoutManager.AutoHideGroups.SelectMany(ahGroup => ahGroup.Items).OfType<LayoutPanel>())
                    {
                        lp.ShowCaption = true;
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }
            }));

No comments: