private void contextMenu_BuildMenu(bool pbRebuild = true)
        {
            //try
            //{            // Remove any old column name items
            for (int i = theContextMenu.Items.Count - 1; i > 0; i--)
            {
                if (((MenuItem)theContextMenu.Items[i]).FontWeight != FontWeights.Bold)
                {
                    theContextMenu.Items.Remove(theContextMenu.Items[i]);
                }
            }

            nBaseItems = theContextMenu.Items.Count;

            // Attach the context menu to the DataGrid ColumnHeaders
            var headersPresenter = WPFDataGridConfigurationBehaviorFinder.FindChild <DataGridColumnHeadersPresenter>(dataGrid);

            if (headersPresenter == null)
            {
                return; // dirty hack to fix a creahs when loading first different pages than the one containing this element
            }
            ContextMenuService.SetContextMenu(headersPresenter, theContextMenu);

            if (VisibleColumns == null)
            {
                throw (new SettingsPropertyNotFoundException("User's VisibleColumns setting not found."));
            }

            // Get the current column ordering from user.config

            if (DisplayIndexes == null)
            {
                throw (new SettingsPropertyNotFoundException("User's DisplayIndexes setting not found."));
            }

            AllColumnDisplayIndexes = DisplayIndexes;
            string[] colIndexes = AllColumnDisplayIndexes.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            System.Linq.IOrderedEnumerable <DataGridColumn> dataGridColumns = dataGrid.Columns.OrderBy(x => x.DisplayIndex);

            // Sort the columns in display index order so menu header order matchs display column order
            if (pbRebuild == false)
            {
                // Initially the datagrid column order is such that display indexes are the same as the col indexes
                if (colIndexes.Length > 0)
                {
                    dataGridColumns = dataGrid.Columns.OrderBy(x => Convert.ToInt16(colIndexes[x.DisplayIndex]));
                }
            }

            if (mnuAlpha.IsChecked)
            {
                dataGridColumns = dataGrid.Columns.OrderBy(x => x.Header.ToString());
            }

            System.Linq.IOrderedEnumerable <DataGridColumn> filteredDataGridColumns = null;
            if (wordFilter.Length > 1)
            {
                filteredDataGridColumns = dataGrid.Columns
                                          .Where(x => x.Header.ToString().Contains(wordFilter) == true).OrderBy(x => x.Header.ToString());
            }
            if (filteredDataGridColumns != null)
            {
                dataGridColumns = filteredDataGridColumns;
            }

            if (wordFilter.Length == 1)
            {
                dataGridColumns = dataGrid.Columns
                                  .Where(x => x.Header.ToString().ToUpper().StartsWith(wordFilter.ToUpper()) == true).OrderBy(x => x.Header.ToString());
            }

            AllColumnsHeaders = "";
            foreach (DataGridColumn col in dataGridColumns)
            {
                // All column name to a list of all column headers for later use.
                AllColumnsHeaders = String.Format("{0};{1}", col.Header.ToString().Replace("\n", " ").Replace("\r", " "), AllColumnsHeaders);

                // Add new menu item in display order.
                contextMenu_AddNewMenuItem(col);
            }

            string sTemp = VisibleColumns;

            VisibleColumns = null;
            VisibleColumns = sTemp;
            //}
            //catch (Exception ex)
            //{ StdErrOut(ex, MethodInfo.GetCurrentMethod().Name); }
        }