private void UpdateTotals()
 {
     foreach (DataGridColumn column in this.AssociatedObject.Columns)
     {
         if (column is DataGridBoundColumn && column is DataGridNumericColumn)
         {
             DataGridNumericColumn numericColumn = (DataGridNumericColumn)column;
             if (numericColumn.IsTotaled)
             {
                 numericColumn.Total = this.sourceCollectionView.OfType <object>().AsParallel().Sum(item => numericColumn.Getter(item));
             }
         }
     }
 }
        //private void LoadData(string columnSet)
        //{
        //	var dataGrid =  TradeLink.DBContext.DBService.Data.DataGridGroups.Expand("DataGridColumns").Where(p => p.Name == columnSet).FirstOrDefault();
        //	this.CreateColumn(dataGrid);
        //}

        //private void CreateColumn(DataGridGroup dataGrid)
        //{
        //	var dgcg = new DataGridColumnGen();
        //	foreach (var column in dataGrid.DataGridColumns)
        //	{
        //		dgcg.Width = column.Width;
        //		dgcg.Type = column.Type;
        //		dgcg.Sequence = column.Sequence;
        //		dgcg.UpdateTriggerId = column.UpdateTriggerId;
        //		dgcg.HasFilter = column.HasFilter;
        //		dgcg.Header = column.Header;
        //		dgcg.Path = column.Path;
        //		dgcg.Format = column.Format;
        //		dgcg.CellTemplate = column.CellTemplate;
        //		dgcg.CellStyle = column.CellStyle;
        //		if (column.Binding_Id.HasValue)
        //			dgcg.Binding_Id = column.Binding_Id.Value;

        //		this.GenerateDataGridColumn(dgcg);
        //	}
        //}

        public void GenerateDataGridColumn(DataGridColumnGen column)
        {
            System.Windows.Controls.DataGridColumn col = null;

            switch (column.Type)
            {
            case 1:        //Text
                col = new Glass.UX.Windows.Controls.DataGridTextColumn();
                break;

            case 2:        //Combo
                col = new Glass.UX.Windows.Controls.DataGridComboBoxColumn();
                break;

            case 3:        //Check
                col = new Glass.UX.Windows.Controls.DataGridCheckBoxColumn();
                break;

            case 4:        //Numeric
                col = new DataGridNumericColumn();
                break;

            case 5:        //Currency
                col = new DataGridNumericColumn();
                break;

            case 6:        //Date
                col = new DataGridDateColumn();
                break;

            case 7:        //Template
                col = new DataGridTemplateColumn();
                ((DataGridTemplateColumn)col).CellTemplate = AssociatedObject.TryFindResource(column.CellTemplate) as DataTemplate;
                break;

            case 8:        //Details
                col       = new DataGridDetailColumn();
                col.Width = column.Width;
                break;
            }

            if (col != null)
            {
                Binding binding = null;
                if (!String.IsNullOrEmpty(column.Path))
                {
                    binding = new Binding(column.Path);
                }
                if (!String.IsNullOrEmpty(column.Format) && binding != null)
                {
                    binding.StringFormat = column.Format;
                }
                if (!String.IsNullOrEmpty(column.Header))
                {
                    col.Header = column.Header;
                }
                if (!String.IsNullOrEmpty(column.CellStyle))
                {
                    col.CellStyle = AssociatedObject.TryFindResource(column.CellStyle) as Style;
                }

                if (column.HasFilter)
                {
                    col.HeaderStyle = Application.Current.Resources["DataGridColumnFilterHeaderStyle"] as Style;
                }

                if (col is DataGridBoundColumn)
                {
                    ((DataGridBoundColumn)col).Binding = binding;
                }
                AssociatedObject.Columns.Add(col);
            }
        }
        public void OnLoad(DockPanel summaryRow)
        {
            if (summaryRow == null)
            {
                return;
            }

            this.summaryRow = summaryRow;

            this.sourceCollectionView = this.AssociatedObject.ItemsSource as ICollectionView;
            if (this.sourceCollectionView == null)
            {
                this.sourceCollectionView = CollectionViewSource.GetDefaultView(this.AssociatedObject.ItemsSource);
            }

            if (this.sourceCollectionView == null)
            {
                return;
            }

            this.sourceCollectionView.CollectionChanged += OnNotifyCollectionChanged;
            ReadOnlyCollection <ItemPropertyInfo> itemProperties = ((IItemProperties)sourceCollectionView).ItemProperties;

            foreach (DataGridColumn column in this.AssociatedObject.Columns)
            {
                DockPanel stackPanel = new DockPanel();
                stackPanel.HorizontalAlignment = HorizontalAlignment.Left;
                TextBlock pipe = new TextBlock();
                pipe.Text = "|";

                stackPanel.Children.Add(pipe);
                TextBlock element = new TextBlock();
                element.Padding       = new Thickness(0, 0, 2, 0);
                element.TextAlignment = TextAlignment.Right;

                stackPanel.Children.Add(element);

                stackPanel.Background = Brushes.Bisque;

                Binding widthBinding = new Binding("ActualWidth");
                widthBinding.Mode   = BindingMode.OneWay;
                widthBinding.Source = column;
                BindingOperations.SetBinding(stackPanel, StackPanel.WidthProperty, widthBinding);

                summaryRow.Children.Add(stackPanel);

                if (column is DataGridBoundColumn && column is DataGridNumericColumn)
                {
                    DataGridNumericColumn numericColumn = (DataGridNumericColumn)column;
                    if (numericColumn.IsTotaled)
                    {
                        ItemPropertyInfo       propertyInfo = itemProperties.FirstOrDefault(item => item.Name == (string)column.Header);
                        Func <object, object>  getterItem   = ((PropertyDescriptor)(propertyInfo.Descriptor)).GetValue;
                        Func <object, decimal> getter       = row => ((decimal)getterItem(row));
                        numericColumn.Getter = getter;

                        Binding binding = new Binding("Total");
                        binding.Mode         = BindingMode.OneWay;
                        binding.StringFormat = numericColumn.Binding.StringFormat;
                        binding.Source       = column;

                        element.SetBinding(TextBlock.TextProperty, binding);
                    }
                }
            }
        }