Completed
Last Updated: 04 Oct 2016 06:36 by Alex Dybenko
Alex Dybenko
Created on: 09 Sep 2016 12:12
Category: PivotGrid
Type: Feature Request
2
ADD. RadPivotGrid - values context menu of RadPivotFieldList not working when using QueryableDataProvider
Workaround:

public Form1()
{
    InitializeComponent();

    this.radPivotFieldList1.ValuesControl.CreatingVisualListItem += Aggregates_CreatingVisualListItem;
}

private void Aggregates_CreatingVisualListItem(object sender, Telerik.WinControls.UI.CreatingVisualListItemEventArgs args)
{
    args.VisualItem = new MyPivotFieldListVisualItem(this.radPivotFieldList1.ViewModel);
}

    public class MyPivotFieldListVisualItem : PivotFieldListVisualItem
    {
        private FieldListViewModel viewModel;
 
        private CommandBarDropDownButton button;
        private RadMenuItem sumMenuItem;
        private RadMenuItem avgMenuItem;
        private RadMenuItem countMenuItem;
 
        public MyPivotFieldListVisualItem(FieldListViewModel viewModel)
            : base(viewModel)
        {
            this.viewModel = viewModel;
        }
 
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
 
            this.sumMenuItem = this.GetType().BaseType
                .GetField("sumMenuItem", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as RadMenuItem;
 
            this.avgMenuItem = this.GetType().BaseType
              .GetField("avgMenuItem", BindingFlags.Instance | BindingFlags.NonPublic)
              .GetValue(this) as RadMenuItem;
 
            this.countMenuItem = this.GetType().BaseType
              .GetField("countMenuItem", BindingFlags.Instance | BindingFlags.NonPublic)
              .GetValue(this) as RadMenuItem;
 
            if (sumMenuItem != null && avgMenuItem != null && countMenuItem != null)
            {
                sumMenuItem.Click += OnAggregateFunctionClick;
                avgMenuItem.Click += OnAggregateFunctionClick;
                countMenuItem.Click += OnAggregateFunctionClick;
            }
 
            this.button = this.GetType().BaseType
             .GetField("button", BindingFlags.Instance | BindingFlags.NonPublic)
             .GetValue(this) as CommandBarDropDownButton;
 
            RadMenuItem aggregateOptionsMenuItem = this.GetType().BaseType
                .GetField("aggregateOptionsMenuItem", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as RadMenuItem;
 
            aggregateOptionsMenuItem.Click += aggregateOptionsMenuItem_Click;
 
            RadMenuItem numberFormatMenuItem = this.GetType().BaseType
                .GetField("numberFormatMenuItem", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as RadMenuItem;
 
            numberFormatMenuItem.Click += numberFormatMenuItem_Click;
        }
 
        private void numberFormatMenuItem_Click(object sender, EventArgs e)
        {
            Value value = (this.Data.DataBoundItem as Value);
            if (value == null)
            {
                return;
            }
 
            MyNumberFormatOptionsDialog dialog =  new MyNumberFormatOptionsDialog();
            dialog.ThemeName = this.GetThemeName();
 
            QueryableAggregateDescription queryableAggregateDescription = (value.Description as QueryableAggregateDescription);
            if (queryableAggregateDescription != null)
            {
                dialog.LoadQueryableSettings(queryableAggregateDescription);
            }
 
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();
 
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                queryableAggregateDescription.StringFormat = dialog.SelectedStringFormat;
                this.viewModel.ExecuteUpdate();
            }
        }
 
        private void aggregateOptionsMenuItem_Click(object sender, EventArgs e)
        {
            MyAggregateOptionsDialog dialog = new MyAggregateOptionsDialog();
            dialog.ThemeName = this.GetThemeName();
 
            Value value = (this.Data.DataBoundItem as Value);
            if (value != null)
            {
                QueryablePropertyAggregateDescriptionBase queryableAggregateDescription = (value.Description as QueryablePropertyAggregateDescriptionBase);
                if (queryableAggregateDescription != null)
                {
                    dialog.LoadQueryableSettings(queryableAggregateDescription);
                }
 
                System.Windows.Forms.DialogResult result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    queryableAggregateDescription.AggregateFunction = dialog.SelectedQueryableAggregateFunction;
                    queryableAggregateDescription.CustomName = dialog.CustomName;
 
                    this.Synchronize();
                    this.viewModel.ExecuteUpdate();
                }
            }
        }
 
        private void OnAggregateFunctionClick(object sender, EventArgs e)
        {
            Value value = (this.Data.DataBoundItem as Value);
            string function = ((RadMenuItem)sender).Text;
 
            if (value != null && this.SetNewAggregateFunction(value.Description, function))
            {
                this.Synchronize();
                this.viewModel.ExecuteUpdate();
            }
        }
 
        private bool SetNewAggregateFunction(IAggregateDescription description, string function)
        {
            QueryablePropertyAggregateDescriptionBase queryableDescription = (description as QueryablePropertyAggregateDescriptionBase);
            if (queryableDescription != null)
            {
                switch (function)
                {
                    case "Sum":
                        queryableDescription.AggregateFunction = QueryableAggregateFunction.Sum;
                        break;
                    case "Count":
                        queryableDescription.AggregateFunction = QueryableAggregateFunction.Count;
                        break;
                    case "Average":
                        queryableDescription.AggregateFunction = QueryableAggregateFunction.Average;
                        break;
                    default:
                        return false;
                }
 
                return true;
            }
 
            return false;
        }
 
        protected override void SynchronizeProperties()
        {
            base.SynchronizeProperties();
 
            Value value = (this.Data.DataBoundItem as Value);
            Label label = (this.Data.DataBoundItem as Label);
 
            if (value != null)
            {
#if !NETFX2
                QueryablePropertyAggregateDescription queryableDescription = (value.Description as QueryablePropertyAggregateDescription);
                if (queryableDescription != null)
                {
                    this.sumMenuItem.IsChecked = queryableDescription.AggregateFunction == QueryableAggregateFunction.Sum;
                    this.countMenuItem.IsChecked = queryableDescription.AggregateFunction == QueryableAggregateFunction.Count;
                    this.avgMenuItem.IsChecked = queryableDescription.AggregateFunction == QueryableAggregateFunction.Average;
                }
#endif
            }
        }
 
        private string GetThemeName()
        {
            if (this.ElementTree == null)
            {
                return string.Empty;
            }
 
            RadControl control = this.ElementTree.Control as RadControl;
 
            return control != null ? control.ThemeName : string.Empty;
        }
    }
 
public class MyAggregateOptionsDialog : AggregateOptionsDialog
    {
        private RadListControl listAggregateFunctions;
        private RadLabel labelFieldName;
        private RadTextBox textBoxCustomName;

        public MyAggregateOptionsDialog()
        {
            this.listAggregateFunctions = this.GetType().BaseType
               .GetField("listAggregateFunctions", BindingFlags.Instance | BindingFlags.NonPublic)
               .GetValue(this) as RadListControl;

            this.labelFieldName = this.GetType().BaseType
                .GetField("labelFieldName", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as RadLabel;

            this.textBoxCustomName = this.GetType().BaseType
                .GetField("textBoxCustomName", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as RadTextBox;
        }

        private readonly IList<QueryableAggregateFunction> availableQueryableAggregateFunctions = new List<QueryableAggregateFunction>()
            {
                QueryableAggregateFunction.Sum,
                QueryableAggregateFunction.Count,
                QueryableAggregateFunction.Average,
                QueryableAggregateFunction.Max,
                QueryableAggregateFunction.Min
            };

        public virtual QueryableAggregateFunction SelectedQueryableAggregateFunction
        {
            get
            {
                return (QueryableAggregateFunction)this.listAggregateFunctions.SelectedValue;
            }
        }

        public virtual void LoadQueryableSettings(QueryablePropertyAggregateDescriptionBase aggregateDescription)
        {
            if (this.listAggregateFunctions.DataSource != null)
            {
                this.listAggregateFunctions.DataSource = availableQueryableAggregateFunctions;
            }

            this.listAggregateFunctions.SelectedValue = aggregateDescription.AggregateFunction;
            this.labelFieldName.Text = aggregateDescription.PropertyName;
            this.Text = String.Format("{0}{1}{2}", "Aggregate Options (", aggregateDescription.PropertyName, ")");
            this.textBoxCustomName.Text = aggregateDescription.CustomName;
        }
    }

public class MyNumberFormatOptionsDialog : NumberFormatOptionsDialog
    {
        private TextBox textBox;

        public MyNumberFormatOptionsDialog()
        {
            RadDropDownList dropDownListFormat = this.GetType().BaseType
                   .GetField("dropDownListFormat", BindingFlags.Instance | BindingFlags.NonPublic)
                   .GetValue(this) as RadDropDownList;

            this.textBox = (TextBox)dropDownListFormat.DropDownListElement.EditableElement.TextBox.TextBoxItem.HostedControl;
        }

        public virtual void LoadQueryableSettings(QueryableAggregateDescriptionBase aggregateDescription)
        {
            this.textBox.Text = aggregateDescription.StringFormat;
            this.Text = String.Format(PivotGridLocalizationProvider.CurrentProvider.GetLocalizedString(PivotStringId.NumberFormatOptionsDialogFormatOptionsDescription),
                aggregateDescription.DisplayName);
            this.textBox.Select(this.textBox.Text.Length, 0);
            this.textBox.Focus();
        }
    }
Attached Files:
0 comments