Aggregate Functions on Context Menu

2 posts, 0 answers
  1. Irwing
    Irwing avatar
    2 posts
    Member since:
    Jan 2012

    Posted 09 Jan 2012 Link to this post

    Is it possible to create a dynamic contextmenu with aggregates functions for each column?
  2. Irwing
    Irwing avatar
    2 posts
    Member since:
    Jan 2012

    Posted 10 Jan 2012 Link to this post

    I wrote a code like this based on your trial example, does it works?

    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Data;
    using System.ComponentModel;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.GridView;
    using Telerik.Windows;
    using Telerik.Windows.Data;
     
     
    namespace esAccessCenterControls
    {
        public class GridViewHeaderMenu
        {
            private RadGridView grid = null;
            static ColumnGroupDescriptor g = new ColumnGroupDescriptor();
     
            public GridViewHeaderMenu(RadGridView grid)
            {
                this.grid = grid;
            }
     
            public static readonly DependencyProperty IsEnabledProperty
                = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(GridViewHeaderMenu),
                    new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
     
            public static void SetIsEnabled(DependencyObject dependencyObject, bool enabled)
            {
                dependencyObject.SetValue(IsEnabledProperty, enabled);
            }
     
            public static bool GetIsEnabled(DependencyObject dependencyObject)
            {
                return (bool) dependencyObject.GetValue(IsEnabledProperty);
            }
     
            public static void SetGroupDescriptor(ref ColumnGroupDescriptor groupDescriptor)
            {
                g = groupDescriptor;
            }
     
            private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
            {
                RadGridView grid = dependencyObject as RadGridView;
                if (grid != null)
                {
                    if ((bool) e.NewValue)
                    {
                        // Create new GridViewHeaderMenu and attach RowLoaded event.
                        GridViewHeaderMenu menu = new GridViewHeaderMenu(grid);
                        menu.Attach();
                    }
                }
            }
     
            private void Attach()
            {
                if (grid != null)
                {
                    // create menu
                    RadContextMenu contextMenu = new RadContextMenu();
                    // set menu Theme
                    StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(grid));
     
                    contextMenu.Opened += OnMenuOpened;
                    contextMenu.ItemClick += OnMenuItemClick;
     
                    RadContextMenu.SetContextMenu(grid, contextMenu);
                }
            }
     
            void OnMenuOpened(object sender, RoutedEventArgs e)
            {
                RadContextMenu menu = (RadContextMenu) sender;
                GridViewHeaderCell cell = menu.GetClickedElement<GridViewHeaderCell>();
     
                if (cell != null)
                {
                    menu.Items.Clear();
     
                    RadMenuItem item = new RadMenuItem();
                    item.Header = "Funções:";
                    menu.Items.Add(item);
     
                    RadMenuItem subMenu1 = new RadMenuItem();
                    subMenu1.Header = "Média total";
                    subMenu1.IsCheckable = true;
                    subMenu1.IsChecked = false;
                    item.Items.Add(subMenu1);
     
                    RadMenuItem subMenu2 = new RadMenuItem();
                    subMenu2.Header = "Soma total";
                    subMenu2.IsCheckable = true;
                    subMenu2.IsChecked = false;
                    item.Items.Add(subMenu2);
     
                    RadMenuItem subMenu3 = new RadMenuItem();
                    subMenu3.Header = "Contar entradas";
                    subMenu3.IsCheckable = true;
                    subMenu3.IsChecked = false;
                    item.Items.Add(subMenu3);
     
                    RadMenuItem subMenu4 = new RadMenuItem();
                    subMenu4.Header = "Maior valor";
                    subMenu4.IsCheckable = true;
                    subMenu4.IsChecked = false;
                    item.Items.Add(subMenu4);
     
                    RadMenuItem subMenu5 = new RadMenuItem();
                    subMenu5.Header = "Menor valor";
                    subMenu5.IsCheckable = true;
                    subMenu5.IsChecked = false;
                    item.Items.Add(subMenu5);
     
                    RadMenuItem subMenu6 = new RadMenuItem();
                    subMenu5.Header = "Limpar funções";
                    subMenu5.IsCheckable = true;
                    subMenu5.IsChecked = false;
                    item.Items.Add(subMenu5);
     
                    item = new RadMenuItem();
                    item.Header = "Exibir Colunas:";
                    menu.Items.Add(item);
     
                    // create menu items
                    foreach (GridViewColumn column in grid.Columns)
                    {
                        RadMenuItem subMenu = new RadMenuItem();
                        subMenu.Header = column.Header;
                        subMenu.IsCheckable = true;
                        subMenu.IsChecked = true;
     
                        Binding isCheckedBinding = new Binding("IsVisible");
                        isCheckedBinding.Mode = BindingMode.TwoWay;
                        isCheckedBinding.Source = column;
     
                        // bind IsChecked menu item property to IsVisible column property
                        subMenu.SetBinding(RadMenuItem.IsCheckedProperty, isCheckedBinding);
     
                        item.Items.Add(subMenu);
                    }
                }
                else
                {
                    menu.IsOpen = false;
                }
            }
     
            void OnMenuItemClick(object sender, RoutedEventArgs e)
            {
                RadContextMenu menu = (RadContextMenu) sender;
     
                GridViewHeaderCell cell = menu.GetClickedElement<GridViewHeaderCell>();
                RadMenuItem clickedItem = ((RadRoutedEventArgs) e).OriginalSource as RadMenuItem;
                GridViewColumn column = cell.Column;
     
                if (clickedItem.Parent is RadMenuItem && ((RadMenuItem)clickedItem.Parent).Header == "Exibir Colunas:")
                    return;
     
                string header = Convert.ToString(clickedItem.Header);
     
                using (grid.DeferRefresh())
                {
                    ColumnSortDescriptor sd = (from d in grid.SortDescriptors.OfType<ColumnSortDescriptor>()
                                               where object.Equals(d.Column, column)
                                               select d).FirstOrDefault();
     
                    if (header.Contains("Média Total"))
                    {
                        if (clickedItem.IsChecked)
                        {
                            AverageFunction a = new AverageFunction();
                            a.Caption = "Média de " + column.Header + ": ";
                            a.SourceField = column.UniqueName;
                            g.Column.AggregateFunctions.Add(a);
                            clickedItem.IsChecked = false;
                        }
                        else
                        {
                            foreach (AggregateFunction func in g.Column.AggregateFunctions)
                            {
                                if (func.Caption.Substring(0, 5) == header.Substring(0, 5))
                                {
                                    g.Column.AggregateFunctions.Remove(func);
                                    clickedItem.IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (header.Contains("Soma total"))
                    {
                        if (clickedItem.IsChecked)
                        {
                            SumFunction s = new SumFunction();
                            s.Caption = "Soma de " + column.Header + ": ";
                            s.SourceField = column.UniqueName;
                            g.Column.AggregateFunctions.Add(s);
                            clickedItem.IsChecked = false;
                        }
                        else
                        {
                            foreach (AggregateFunction func in g.Column.AggregateFunctions)
                            {
                                if (func.Caption.Substring(0, 4) == header.Substring(0, 4))
                                {
                                    g.Column.AggregateFunctions.Remove(func);
                                    clickedItem.IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (header.Contains("Contar entradas"))
                    {
                        if (clickedItem.IsChecked)
                        {
                            CountFunction c = new CountFunction();
                            c.Caption = "Quantidade de " + column.Header + ": ";
                            g.Column.AggregateFunctions.Add(c);
                            clickedItem.IsChecked = false;
                        }
                        else
                        {
                            foreach (AggregateFunction func in g.Column.AggregateFunctions)
                            {
                                if (func.Caption.Substring(0, 6) == header.Substring(0, 6))
                                {
                                    g.Column.AggregateFunctions.Remove(func);
                                    clickedItem.IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (header.Contains("Maior valor"))
                    {
                        if (clickedItem.IsChecked)
                        {
                            MaxFunction m = new MaxFunction();
                            m.Caption = "Maior valor de " + column.Header + ": ";
                            g.Column.AggregateFunctions.Add(m);
                            clickedItem.IsChecked = false;
                        }
                        else
                        {
                            foreach (AggregateFunction func in g.Column.AggregateFunctions)
                            {
                                if (func.Caption.Substring(0, 5) == header.Substring(0, 5))
                                {
                                    g.Column.AggregateFunctions.Remove(func);
                                    clickedItem.IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (header.Contains("Menor valor"))
                    {
                        if (clickedItem.IsChecked)
                        {
                            MinFunction m = new MinFunction();
                            m.Caption = "Menor valor de " + column.Header + ": ";
                            g.Column.AggregateFunctions.Add(m);
                            clickedItem.IsChecked = false;
                        }
                        else
                        {
                            foreach (AggregateFunction func in g.Column.AggregateFunctions)
                            {
                                if (func.Caption.Substring(0, 5) == header.Substring(0, 5))
                                {
                                    g.Column.AggregateFunctions.Remove(func);
                                    clickedItem.IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (header.Contains("Limpar funções"))
                    {
                        g.Column.AggregateFunctions.Clear();
                    }
                }
            }
        }
    }
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top