locked
DataGrid command bar strangeness! RRS feed

  • Question

  • Hi all,

    When I wanted to change the appearance ( specifically hiding/showing the datagrid header and/or commandbar ) of a grid pre-cosmopolitan shell/theme, I was able to write code like this, called from the grid's ControlAvailable event:

            public static void SetGridAttributes ( DataGrid dgr, bool hideHeader, bool hideCommandBar, bool setReadOnlyColsNoTab, bool useExtendedMode )
            {
                if (hideHeader || hideCommandBar)
                {
                    // get a reference to framework elements of the Grid
                    Grid grid = (Grid)((FrameworkElement)((FrameworkElement)(((FrameworkElement)(dgr)).Parent)).Parent).Parent;
    
                    if (hideHeader)
                    {
                        // Remove Grid Header
                        grid.Children[0].Visibility = System.Windows.Visibility.Collapsed;
                    }
                    if (hideCommandBar)
                    {
                        // Remove Grid Command Bar
                        grid.Children[1].Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
            }

    All worked fine. With Cosmo however, hiding grid.Children[0] now hides both the header AND the command bar at the top, so now I can't have my buttons showing WITHOUT a header. I use this scenario when I have grids on tabs and the tab displays what the grid actually is, without repeating the same information on the grid header just below. So now I cannot do this.

    Bizzarely ( in my view ! ), hiding grid.Children[1], which used to be the 'old' commandbar section, now actually hides any paging controls ( if paging is set ) at the bottom of the grid!

    Does anyone have a clue as to how to do what I want (i.e. have header/command buttons visible/not visible in any combination, as was possible before Cosmo?

    Thanks!


    Ian Mac

    Thursday, May 9, 2013 9:55 AM

Answers

  • Well I answered my own question!

            public static void SetGridAttributes ( DataGrid dgr, bool hideHeader, bool hideCommandBar )
            {
                if (hideHeader || hideCommandBar)
                {
                    // Get a reference to the framework element that equates to the FIRST-level Grid in the XAML for a
                    // DataGrid ( which is described in DataGridStyles.xaml as <Style TargetType="ctl:DataGridVisual">,
                    // using the passed-in DataGrid control 'dgr' as a starting point
                    Grid grid = (Grid)((FrameworkElement)((FrameworkElement)(((FrameworkElement)(dgr)).Parent)).Parent).Parent;
    
                    if (hideHeader)
                    {
                        // HIDE DataGrid TITLE header
                        ((TextBlock)((Grid)((Border)grid.Children[0]).Child).Children[0]).Visibility = Visibility.Collapsed;
    
                    }
                    if (hideCommandBar)
                    {
                        //grid.Children[1].Visibility = System.Windows.Visibility.Collapsed;
                        ((CommandGroupPresenter)((Grid)((Border)grid.Children[0]).Child).Children[1]).Visibility = Visibility.Collapsed;
                    }
                }
            }

    Yes I know it's horrible but I couldn't find another way to reference the controls in the time I had. The only reason I was able to get to this point is that I could trawl through the source code for the Cosmo theme, which of course was released last year ( where, please, are those promised blog posts on adapting the shell/theme for our needs ? ). It was not intuitive and a hell of a lot of debugging/breakpointing/immediate windowing(?) was required to get there.

    A point to emphasise. This is specifically for use only if you are using the Cosmo theme, or rather, it's the only one I've tested it on and, given the template hierarchy, is unlikely to work with any other! The previous code as I said worked fine with the 'standard' LS theme.

    If you're interested, the file I trawled is Cosmoploitan.Client\Presentation\Themes\DataGridStyles.xaml and the section you want to look at begins with '<Style TargetType="ctl:DataGridVisual">'. Somewhere below you will find a section that begins '<!--Title-->' and if you look at the following 'border' then 'grid' definitions you will find in that grid a TextBlock which displays the header/title and then immediately the CommandGroupPresenter which obviously is the commad bar. These are the two elements I've referenced in the code above.

    Hope it helps!


    Ian Mac

    • Marked as answer by Ian Mac Thursday, May 9, 2013 2:42 PM
    Thursday, May 9, 2013 2:41 PM