internal SelectedCellsChangedEventArgs(DataGrid owner, VirtualizedCellInfoCollection addedCells, VirtualizedCellInfoCollection removedCells)
        {
            _addedCells = (addedCells != null) ? addedCells : VirtualizedCellInfoCollection.MakeEmptyCollection(owner);
            _removedCells = (removedCells != null) ? removedCells : VirtualizedCellInfoCollection.MakeEmptyCollection(owner);

            Debug.Assert(_addedCells.IsReadOnly, "_addedCells should have ended up as read-only.");
            Debug.Assert(_removedCells.IsReadOnly, "_removedCells should have ended up as read-only.");
        }
        internal DataGridCellInfo(object item, DataGridColumn column, DataGrid owner)
        {
            Debug.Assert(item != null, "item should not be null.");
            Debug.Assert(column != null, "column should not be null.");
            Debug.Assert(owner != null, "owner should not be null.");

            _item = item;
            _column = column;
            _owner = new WeakReference(owner);
        }
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:

            #line 4 "..\..\..\UserControls\ColumnChooserControl.xaml"
                ((ExtendedGrid.UserControls.ColumnChooserControl)(target)).SizeChanged += new System.Windows.SizeChangedEventHandler(this.UserControlSizeChanged);

            #line default
            #line hidden
                return;

            case 4:
                this.popupDrag = ((System.Windows.Controls.Primitives.Popup)(target));
                return;

            case 5:
                this.grid = ((ExtendedGrid.Microsoft.Windows.Controls.DataGrid)(target));
                return;
            }
            this._contentLoaded = true;
        }
 private void GridLoaded(object sender, RoutedEventArgs e)
 {
     CurrentGrid = (DataGrid) sender;
 }
 private double GetGridHeaderActualWidth(DataGrid grid)
 {
     if (grid.HeadersVisibility == DataGridHeadersVisibility.None || grid.HeadersVisibility == DataGridHeadersVisibility.Column)
         return 0;
     return grid.RowHeaderWidth;
 }
        private void AutoFilterMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {

                e.Handled = true;

                if (e.ChangedButton == MouseButton.Right)
                    return;
                var column = FindControls.FindParent<DataGridColumnHeader>((ContentControl)sender);

                var extendedColumn = column.Column as IExtendedColumn;
                if (extendedColumn != null)
                {
                    if (!extendedColumn.AllowAutoFilter)
                        return;
                }

                var popup = FindControls.FindChild<Popup>(column, "popupDrag");

                if (popup == null) return;
                popup.IsOpen = true;
                //Change the position of popup with the mouse
                var popupSize = new Size(popup.ActualWidth, popup.ActualHeight);
                var position = new Point { X = column.ActualWidth - 19, Y = column.ActualHeight };
                popup.PlacementRectangle = new Rect(position, popupSize);
                var listbox = FindControls.FindChild<ListBox>(popup.Child, "autoFilterList");
                listbox.Focus();
                listbox.LostFocus += PopLostFocus;
                var clearButton = FindControls.FindChild<Button>(popup.Child, "btnClear");
                //Get the data from
                var mainGrid = FindControls.FindParent<ExtendedDataGrid>(popup);
                if (mainGrid != null)
                {
                    mainGrid.CommitEdit();
                    mainGrid.CommitEdit();
                    mainGrid.AutoFilterHelper.CurrentListBox = listbox;
                    CurrentColumn = column.Column.SortMemberPath;
                    CurrentGrid = mainGrid;

                    if (mainGrid.AutoFilterHelper.CurrentColumName == CurrentColumn)
                    {
                        List<CheckedListItem> previousValues = mainGrid.AutoFilterHelper.CurrentDistictValues;
                        var currentValues = mainGrid.AutoFilterHelper.GetDistictValues(CurrentGrid, CurrentColumn);

                        foreach (var checkedListItem in currentValues)
                        {
                            var item = checkedListItem;
                            if (previousValues.Count(c => Convert.ToString(c.Name) == Convert.ToString(item.Name) && c.IsSelectAll != "(Select All)") == 0 && checkedListItem.IsSelectAll != "(Select All)")
                            {
                                previousValues.Add(new CheckedListItem { Name = checkedListItem.Name, IsChecked = previousValues[0].IsChecked });
                            }
                        }

                        if (clearButton != null && mainGrid.AutoFilterHelper.CurrentDistictValues!=null)
                        {
                            clearButton.IsEnabled = mainGrid.AutoFilterHelper.CurrentDistictValues.Count(c => c.IsChecked) > 0;
                            clearButton.UpdateLayout();
                        }
                        mainGrid.AutoFilterHelper.CurrentDistictValues = previousValues;
                        mainGrid.AutoFilterHelper.CurrentDistictValues[0].IsSelectAll = "(Select All)";
                        listbox.ItemsSource = mainGrid.AutoFilterHelper.CurrentDistictValues;
                        listbox.UpdateLayout();
                        listbox.Items.Refresh();
                        return;
                    }

                    mainGrid.AutoFilterHelper.CurrentColumName = CurrentColumn;
                    var distict = mainGrid.AutoFilterHelper.GetDistictValues(CurrentGrid, CurrentColumn);

                    if (distict.Count(c => !c.IsChecked) == distict.Count)
                    {
                        if (Convert.ToString(popup.Tag) == "True")
                        {
                            _isLoading = true;
                            foreach (var checkedListItem in distict)
                            {
                                checkedListItem.IsChecked = true;
                            }
                        }
                    }

                    distict[0].IsSelectAll = "(Select All)";
                    listbox.ItemsSource = distict;
                    if (clearButton != null && mainGrid.AutoFilterHelper.CurrentDistictValues != null)
                    {
                        clearButton.IsEnabled = mainGrid.AutoFilterHelper.CurrentDistictValues.Count(c => c.IsChecked) > 0;

                        clearButton.UpdateLayout();
                    }

                }
            }
            finally
            {

                _isLoading = false;
            }
        }
        private void UnChecked(object sender, RoutedEventArgs e)
        {
            if (_isLoading) return;
            ExtendedDataGrid grid=null;
            try
            {
                _isLoading = true;
                var checkbox = (CheckBox)sender;
                var listBox = FindControls.FindParent<ListBox>(checkbox);
                var popup = (Popup)((StackPanel)(listBox.Parent)).Parent;
                grid  = FindControls.FindParent<ExtendedDataGrid>(popup);
                CurrentGrid = grid;
                grid.AutoFilterHelper.CurrentListBox = listBox;

                var value = checkbox.Content;
                if (Convert.ToString(checkbox.Tag) != "(Select All)")
                {

                    var distictValues = grid.AutoFilterHelper.CurrentListBox.ItemsSource as List<CheckedListItem>;
                    if (distictValues != null)
                    {
                        var countOfFiltersSelected = distictValues.Count(c => c.IsChecked && Convert.ToString(c.Name) != "(Select All)" && c.IsSelectAll != "(Select All)");
                        if (countOfFiltersSelected == distictValues.Count - 1)
                        {
                            _isLoading = true;
                            distictValues[0].IsChecked = false;
                            grid.AutoFilterHelper.CurrentListBox.UpdateLayout();
                            grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                        }
                        bool isFilterApplicable = false;
                        if(grid.ItemsSource is DataView)
                        {
                            isFilterApplicable = (!string.IsNullOrEmpty(((DataView)grid.ItemsSource).RowFilter) && ((DataView)grid.ItemsSource).RowFilter.Contains("[" + CurrentColumn + "]  IN"));
                        }
                        else if (CollectionViewSource.GetDefaultView(grid.ItemsSource) != null)
                        {
                            isFilterApplicable = (!string.IsNullOrEmpty(grid.AutoFilterHelper.FilterExpression) && (grid.AutoFilterHelper.FilterExpression.Contains("[" + CurrentColumn + "]  IN")));
                        }

                        if (!isFilterApplicable && !string.IsNullOrEmpty(Convert.ToString(value)))
                        {
                            string rowFilter;
                            if (grid.ItemsSource is DataView)
                            {
                                rowFilter = ((DataView)grid.ItemsSource).RowFilter;
                                if (string.IsNullOrEmpty(rowFilter))
                                {
                                    var actualValues = ((DataView)CurrentGrid.ItemsSource).Table.Rows.Cast<DataRow>().Select(row => Convert.ToString(row[CurrentColumn])).Where(val => val != Convert.ToString(value)).ToList();

                                    foreach (var actualValue in actualValues)
                                    {
                                        grid.AutoFilterHelper.ApplyFilters(CurrentGrid, CurrentColumn, actualValue);
                                    }

                                    var distictictValues = grid.AutoFilterHelper.CurrentDistictValues;
                                    distictictValues[0].IsChecked = false;
                                    grid.AutoFilterHelper.CurrentDistictValues = distictictValues;
                                    grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                                    popup.Tag = distictictValues.Count(c => c.IsChecked) == 0 ? "False" : "True";
                                    CurrentGrid.Items.Refresh();
                                    CurrentGrid.UpdateLayout();
                                }
                                else
                                {

                                    grid.AutoFilterHelper.CurrentDistictValues[0].IsChecked = countOfFiltersSelected == distictValues.Count - 1;
                                    grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                                    grid.AutoFilterHelper.CurrentListBox.UpdateLayout();
                                    popup.Tag = grid.AutoFilterHelper.CurrentDistictValues.Count(c => c.IsChecked) == 0 ? "False" : "True";
                                    CurrentGrid.Items.Refresh();
                                    CurrentGrid.UpdateLayout();

                                }
                            }
                            else if (CollectionViewSource.GetDefaultView(grid.ItemsSource) != null)
                            {
                                rowFilter = grid.AutoFilterHelper.FilterExpression;
                                if (string.IsNullOrEmpty(rowFilter))
                                {
                                    ICollectionView view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
                                    var actualValues = new HashSet<string>();
                                    foreach (var rowData in grid.ItemsSource)
                                    {
                                        var propertyValue = rowData.GetType().GetProperty(CurrentColumn);
                                        if (propertyValue != null)
                                        {
                                            var data = Convert.ToString(propertyValue.GetValue(rowData, null));
                                            if (!actualValues.Contains(data) && data != Convert.ToString(value))
                                            {
                                                actualValues.Add(data);
                                            }
                                        }
                                    }
                                    foreach (var actualValue in actualValues)
                                    {
                                        grid.AutoFilterHelper.ApplyFilters(CurrentGrid, CurrentColumn, actualValue);
                                    }

                                    var distictictValues = grid.AutoFilterHelper.CurrentDistictValues;
                                    distictictValues[0].IsChecked = false;
                                    grid.AutoFilterHelper.CurrentDistictValues = distictictValues;
                                    grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                                    popup.Tag = distictictValues.Count(c => c.IsChecked) == 0 ? "False" : "True";
                                    grid.Items.Refresh();
                                    grid.UpdateLayout();

                                }
                                else
                                {
                                    grid.AutoFilterHelper.CurrentDistictValues[0].IsChecked = countOfFiltersSelected == distictValues.Count - 1;
                                    grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                                    grid.AutoFilterHelper.CurrentListBox.UpdateLayout();
                                    popup.Tag = grid.AutoFilterHelper.CurrentDistictValues.Count(c => c.IsChecked) == 0 ? "False" : "True";
                                    grid.Items.Refresh();
                                    grid.UpdateLayout();
                                }
                            }

                        }
                        else
                        {
                            grid.AutoFilterHelper.CurrentListBox = FindControls.FindChild<ListBox>(popup.Child, "autoFilterList");
                            grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                            distictValues = grid.AutoFilterHelper.CurrentListBox.ItemsSource as List<CheckedListItem>;
                            if (distictValues != null)
                            {
                                var checkCount = distictValues.Count(c => c.IsChecked && Convert.ToString(c.Name) != "(Select All)" && c.IsSelectAll != "(Select All)");
                                if (checkCount == 0)
                                {
                                    grid.AutoFilterHelper.RemoveAllFilter(CurrentGrid, CurrentColumn);
                                    grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                                    popup.Tag = countOfFiltersSelected == 0 ? "False" : "True";
                                }
                                else
                                {
                                    grid.AutoFilterHelper.RemoveFilters(CurrentGrid, CurrentColumn, value);
                                    var distictictValues = grid.AutoFilterHelper.CurrentDistictValues;
                                    popup.Tag = distictictValues.Count(c => c.IsChecked) == 0 ? "False" : "True";
                                }
                            }
                            var sp1 = FindControls.FindParent<StackPanel>(grid.AutoFilterHelper.CurrentListBox);
                            var popup1 = sp1.Parent as Popup;
                            if (popup1 != null)
                            {
                                var clearButton = FindControls.FindChild<Button>(popup1.Child, "btnClear");
                                if (clearButton != null)
                                {
                                    clearButton.IsEnabled = false;

                                }

                            }
                        }
                    }
                }

                else
                {
                    _isLoading = true;
                    var distictictValues = grid.AutoFilterHelper.CurrentDistictValues;
                    distictictValues[0].IsChecked = false;
                    foreach (var distictictValue in distictictValues)
                    {
                        if (Convert.ToString(distictictValue.Name) != "(Select All)" && distictictValue.IsSelectAll != "(Select All)")
                            distictictValue.IsChecked = false;
                    }
                    grid.AutoFilterHelper.CurrentDistictValues = distictictValues;
                    grid.AutoFilterHelper.RemoveAllFilter(CurrentGrid, CurrentColumn);
                    grid.AutoFilterHelper.CurrentListBox.Items.Refresh();
                    var countOfFiltersSelected = distictictValues.Count(c => c.IsChecked && Convert.ToString(c.Name) != "(Select All)");
                    popup.Tag = countOfFiltersSelected == 0 ? "False" : "True";
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                if (grid != null)
                    grid.Items.Refresh();
                _isLoading = false;
            }
        }
        private void SigmaMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            if (e.ChangedButton == MouseButton.Right)
                return;
            var column = FindControls.FindParent<DataGridColumnHeader>((ContentControl)sender);

            var popup = FindControls.FindChild<Popup>(column, "sigmaDrag");

            if (popup == null) return;
            popup.IsOpen = true;
            //Change the position of popup with the mouse
            var popupSize = new Size(popup.ActualWidth, popup.ActualHeight);
            var position = new Point { X = column.ActualWidth - 19, Y = column.ActualHeight };
            popup.PlacementRectangle = new Rect(position, popupSize);
            var listbox = FindControls.FindChild<ListBox>(popup.Child, "sigmaList");
            listbox.Focus();
            listbox.LostFocus += PopSigmaLostFocus;
            var clearButton = FindControls.FindChild<Button>(popup.Child, "btnSigmaClear");
            //Get the data from
            var mainGrid = FindControls.FindParent<ExtendedDataGrid>(popup);
            if (mainGrid != null)
            {
                mainGrid.CommitEdit();
                mainGrid.CommitEdit();
                mainGrid.AutoFilterHelper.CurrentSigmaListBox = listbox;
                CurrentSigmaColumn = column.Column.SortMemberPath;
                CurrentGrid = mainGrid;

                List<CheckedListItem> currentValues;

                var itemSource = (DataView)mainGrid.ItemsSource;
                var table = itemSource.Table;
                CurrentColumn = column.Column.SortMemberPath;
                if (IsSummable(table.Columns[column.Column.SortMemberPath].DataType))
                currentValues = new List<CheckedListItem>()
                                        {
                                            new CheckedListItem(){Name = "Sum",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Sum)},
                                            new CheckedListItem(){Name = "Average",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Average)},
                                            new CheckedListItem(){Name = "Count",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Count)},
                                            new CheckedListItem(){Name = "Min",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Min)},
                                            new CheckedListItem(){Name = "Max",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Max)}
                                        };
                else
                    currentValues = new List<CheckedListItem>()
                                        {
                                            new CheckedListItem(){Name = "Count",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Count)},
                                            new CheckedListItem(){Name = "Smallest",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Smallest)},
                                            new CheckedListItem(){Name = "Largest",IsChecked = IsSummariesChecked(CurrentColumn,ExtendedDataGrid.Lasrgest)}
                                        };

                if (clearButton != null)
                {
                    clearButton.IsEnabled = currentValues.Count(c => c.IsChecked) > 0;
                    clearButton.UpdateLayout();
                }

                listbox.ItemsSource = currentValues;
                listbox.UpdateLayout();
                listbox.Items.Refresh();
            }
        }
 internal SelectedCellsCollection(DataGrid owner) : base(owner)
 {
 }
        /// <summary>
        ///     This is used strictly to create the partial CellInfos.
        /// </summary>
        /// <remarks>
        ///     This is being kept private so that it is explicit that the
        ///     caller expects invalid data.
        /// </remarks>
        private DataGridCellInfo(DataGrid owner, DataGridColumn column, object item)
        {
            Debug.Assert(owner != null, "owner should not be null.");

            _item = item;
            _column = column;
            _owner = new WeakReference(owner);
        }
 /// <summary>
 ///     Assumes that if the owner matches, then the column and item fields are valid.
 /// </summary>
 internal bool IsValidForDataGrid(DataGrid dataGrid)
 {
     DataGrid owner = Owner;
     return (ArePropertyValuesValid && (owner == dataGrid)) || (owner == null);
 }
        /// <summary>
        ///     This is used by CurrentCell if there isn't a valid CurrentItem or CurrentColumn.
        /// </summary>
        internal static DataGridCellInfo CreatePossiblyPartialCellInfo(object item, DataGridColumn column, DataGrid owner)
        {
            Debug.Assert(owner != null, "owner should not be null.");

            if ((item == null) && (column == null))
            {
                return Unset;
            }
            else
            {
                return new DataGridCellInfo(owner, column, (item == null) ? DependencyProperty.UnsetValue : item);
            }
        }