コード例 #1
0
 public static void SetCell(DependencyObject dependencyObject, ReportCell value)
 {
     dependencyObject.SetValue(DynamicReport.CellProperty, value);
 }
コード例 #2
0
 /// <summary>
 /// This is in response to focused cell change because of Keyboard navigation.
 /// </summary>
 private void FocusCellChanged()
 {
     this.selectedRanges.Clear();
     this.anchorPoint         = this.ReportGrid.CurrentReportCell.Rect.Center();
     this.currentSelectedCell = this.ReportGrid.CurrentReportCell;
 }
コード例 #3
0
        /// <summary>
        /// Overrides the default arrangement of a Canvas.
        /// </summary>
        /// <param name="arrangeSize">The largest possible size for the canvas.</param>
        /// <returns>The actual size used by this canvas.</returns>
        protected override Size MeasureOverride(Size constraint)
        {
            // This calculates the visible area of the virtual canvas.  Note that the 'MeasureOverride' is called before the
            // 'OnRenderSizeChanged' so the rectangle used for clipping can't be used here because it hasn't been calclated
            // properly yet.
            Rect viewport = new Rect(this.rectangleGeometry.Rect.Location, constraint);

            // IMPORTANT CONCEPT: The FrameworkElements are recycled for performance.  Creating the user interface elements and
            // adding and removing them from the canvas are very time consuming tasks.  The XAML for the reports contains templates
            // used to create FrameworkElements for a given CLR type.  It turns out that any CLR object of a given type can use any
            // FrameworkElement created for that given type.  So a factory of sorts is created for building and reusing the
            // framework elements based on a CLR type.  A screen is drawn by first reclaiming all the elements that are no longer
            // visible.  These elements are not removed from the screen in the first pass.  The secod pass draws the visible area
            // of the report using reclaimed elements when it can, creating new FrameworkElements when it can't.  The important
            // performance kick here is that the elements are not actually removed from the screen during the reclaimation pass and
            // the drawing pass.  All elements are put into this list when during the reclaimation cycle.  If they are still in
            // this list after the drawing pass, then they are not visible and can be removed from the canvas. This is the
            // reclamation cycle.  All the user interface elements that have become invisible are pushed back into the element
            // cache where they can be used again when the viewport is updated.
            foreach (UIElement uiElement in this.Children)
            {
                if (uiElement is FrameworkElement)
                {
                    // This element will be examined to see if it needs to be recycled.
                    FrameworkElement frameworkElement = uiElement as FrameworkElement;

                    // The 'ReportCell' is the virtual information behind the user interface element and is attached to all the
                    // visual elements in the canvas.  It is used here to indicate whether the associated FrameworkElement is
                    // invisible.
                    ReportCell reportCell = DynamicReport.GetCell(frameworkElement);
                    try
                    {
                        if (reportCell != null)
                        {
                            // This will test the virtual cell to see if it is invisible.  Note that any cell that touches the right or
                            // bottom edge of the viewport is considered to be invisible.  There is no part of these cells that can
                            // actually be seen, whereas the left and top edges are visible.
                            Rect rect = reportCell.ActualRect;
                            if (((viewport.Top > rect.Top || rect.Top >= viewport.Bottom) &&
                                 (viewport.Top >= rect.Bottom || rect.Bottom > viewport.Bottom)) ||
                                ((viewport.Left > rect.Left || rect.Left >= viewport.Right) &&
                                 (viewport.Left >= rect.Right || rect.Right > viewport.Right)))
                            {
                                // When the framework element has become invisible, it is placed back in the cache based on the CLR
                                // type of the object it can display.  It can be recycled for use with similar types when the new
                                // viewport is constructed in the second pass.
                                Type contentType = reportCell.Content.GetType();
                                Stack <FrameworkElement> elementStack;
                                if (!this.elementCache.TryGetValue(contentType, out elementStack))
                                {
                                    elementStack = new Stack <FrameworkElement>();
                                    this.elementCache.Add(contentType, elementStack);
                                }
                                elementStack.Push(frameworkElement);

                                // This disconnects the visual element from the virtual ReportCell and the virtual ReportCell from the
                                // visual element.
                                DynamicReport.SetCell(frameworkElement, ReportCell.Empty);
                                this.elementTable.Remove(reportCell);

                                // The focus scope for this canvas must be cleared when the element that had the focus is recycled.
                                // This method 'virtualizes' the handling of the focus.  The virtual focus is saved in the 'IsFocused'
                                // property of the ReportCell while the cell is invisible.  When this virtual cell becomes visible
                                // again, the keyboard focus will be given back to to user interface element used to instantiate the
                                // virtual cell.
                                if (reportCell.IsFocused)
                                {
                                    FocusManager.SetFocusedElement(this.ReportGrid, null);
                                    if (frameworkElement.IsKeyboardFocusWithin)
                                    {
                                        Keyboard.Focus(this.ReportGrid);
                                    }
                                }

                                // All these bindings need to be released when a cell is no longer visible.  There seems to be a very
                                // practical limitation to how many binding updates can be done by the operating system.  Failure to
                                // release a binding when a user element is no longer visible will quickly result all the resources
                                // being used.
                                BindingOperations.ClearBinding(frameworkElement, Canvas.LeftProperty);
                                BindingOperations.ClearBinding(frameworkElement, Canvas.TopProperty);
                                BindingOperations.ClearBinding(frameworkElement, FrameworkElement.WidthProperty);
                                BindingOperations.ClearBinding(frameworkElement, FrameworkElement.HeightProperty);
                                BindingOperations.ClearBinding(frameworkElement, DynamicReport.IsEvenProperty);
                                BindingOperations.ClearBinding(frameworkElement, DynamicReport.IsSelectedProperty);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        String message = reportCell.ReportColumn == null ? "ReportColumn is null" : reportCell.ReportRow == null ? "ReportRow is null" : "not sure why";
                        EventLog.Error("{0}: {1} ({2})\n{3}", exception.GetType(), exception.Message, message, exception.StackTrace);
                    }
                }
            }

            // This pass will construct the visible part of the canvas.  The main idea is to find the virtual cells that appear in
            // the viewport and associate the cell with a visual element.  If a visual element can be found in the cache, it is
            // used, otherwise one is created from a template.
            foreach (ReportRow reportRow in this.ReportGrid.Rows)
            {
                if (reportRow.IsEmpty)
                {
                    continue;
                }

                // Only rows that appear in the visible part of the canvas are considered.  Evaluating the rows this way quickly
                // removes the number of cells that need to be checked.
                if (((viewport.Top <= reportRow.Top && reportRow.Top < viewport.Bottom) ||
                     (viewport.Top < reportRow.Bottom && reportRow.Bottom <= viewport.Bottom)))
                {
                    // At this point we have a row that is part of the visible canvas.  This will see which of the columns are
                    // visible.
                    foreach (ReportColumn reportColumn in this.ReportGrid.Columns)
                    {
                        // Note that columns that fall on the right edge are excluded.  Even though they appear to intersect with
                        // the viewport, there is no part of these columns that's actally visible.
                        if ((viewport.Left <= reportColumn.Left && reportColumn.Left < viewport.Right) ||
                            (viewport.Left < reportColumn.Right && reportColumn.Right <= viewport.Right))
                        {
                            // The cell exists at a virtual location in the document coordinate system.  They are instantiated and
                            // added to the canvas when the become visible and are remove from the canvas when not visible.  This
                            // is how a very large document can be viewed with a relatively small number of visual elements and
                            // bindings.
                            ReportCell reportCell = reportRow[reportColumn];

                            // If this cell doesn't have a framework element associated with it, then one is either recycled
                            // from the cache or created from a template.
                            FrameworkElement frameworkElement;
                            if (!this.elementTable.TryGetValue(reportCell, out frameworkElement))
                            {
                                // Each Framework element will work with one and only one CLR type.  The content of the
                                // ReportCell determines what kind of FrameworkElement instance is used to display the data in
                                // that cell.
                                Type contentType = reportCell.Content.GetType();

                                // The CLR type of the content is used to find a stack of FrameworkElements that can be used to
                                // display that content.
                                Stack <FrameworkElement> elementStack;
                                if (!this.elementCache.TryGetValue(contentType, out elementStack))
                                {
                                    elementStack = new Stack <FrameworkElement>();
                                    this.elementCache.Add(contentType, elementStack);
                                }

                                // If there are no instances of a visual element that can be used to display the content of
                                // this ReportCell, then one is generated from the template.  Otherwise, a recycled element is
                                // used.  Note that all default focus styles are removed from visual elements on the canvas as
                                // there these elements have a custom style applied which functions like the Microsoft Excel
                                // focus and selection.
                                if (elementStack.Count == 0)
                                {
                                    DataTemplateKey dataTemplateKey = new DataTemplateKey(reportCell.Content.GetType());
                                    DataTemplate    dataTemplate    = this.ReportGrid.Resources[dataTemplateKey] as DataTemplate;
                                    if (dataTemplate != null)
                                    {
                                        frameworkElement = dataTemplate.LoadContent() as FrameworkElement;
                                        frameworkElement.FocusVisualStyle = null;
                                    }
                                }
                                else
                                {
                                    frameworkElement = elementStack.Pop();
                                }

                                // This associates the visual element with the ReportCell that contains the information about
                                // the location of that element in the virtual document coordinate space.  The 'elementTable'
                                // contains the reciprocal relation.
                                DynamicReport.SetCell(frameworkElement, reportCell);
                                this.elementTable.Add(reportCell, frameworkElement);

                                // The data context for the XAML elements is the content of the cell, not the ReportCell.
                                // This is done intensionally to hide the implementation details of how elements are positioned
                                // on the screen.  It is also done to simplify the programming model for the XAML code.
                                frameworkElement.DataContext = reportCell.Content;

                                // Bind the element's 'Left' property of the element to the column's position.
                                Binding leftBinding = new Binding("ActualLeft");
                                leftBinding.Source = reportCell.ReportColumn;
                                BindingOperations.SetBinding(frameworkElement, Canvas.LeftProperty, leftBinding);

                                // Bind the element's 'Top' property of the element to the row's position.
                                Binding topBinding = new Binding("ActualTop");
                                topBinding.Source = reportCell.ReportRow;
                                BindingOperations.SetBinding(frameworkElement, Canvas.TopProperty, topBinding);

                                // Bind the element's 'Width' property of the element to the column's width.
                                Binding widthBinding = new Binding("ActualWidth");
                                widthBinding.Source = reportCell.ReportColumn;
                                BindingOperations.SetBinding(frameworkElement, FrameworkElement.WidthProperty, widthBinding);

                                // Bind the element's 'Height' property of the element to the row's height.
                                Binding heightBinding = new Binding("ActualHeight");
                                heightBinding.Source = reportCell.ReportRow;
                                BindingOperations.SetBinding(frameworkElement, FrameworkElement.HeightProperty, heightBinding);

                                // Bind the element's 'IsSelected' property to the cell's property.  This property is used to
                                // highlight the selected cells.
                                Binding isSelectedBinding = new Binding("IsSelected");
                                isSelectedBinding.Source = reportCell;
                                BindingOperations.SetBinding(frameworkElement, DynamicReport.IsSelectedProperty, isSelectedBinding);

                                // Bind the 'Height' property of the element to the row's 'IsEven' property.  This property is
                                // generally used to shade every other line to make it easier to follow the information on a
                                // row.
                                Binding evenBinding = new Binding("IsEven");
                                evenBinding.Source = reportCell.ReportRow;
                                BindingOperations.SetBinding(frameworkElement, DynamicReport.IsEvenProperty, evenBinding);
                            }

                            // New and recycled elements need to be added to the canvas to become part of the visible report.
                            if (frameworkElement.Parent == null)
                            {
                                this.Children.Add(frameworkElement);
                            }

                            // The logical (and keyboard) focus is restored to the element that has the virtual focus when the
                            // virtual cell is made visible again.  Note that the logical focus is only moved when the canvas
                            // has the keyboard focus.  This prevents a scenario where the canvas grabs the input focus away
                            // from another window when the input focus is made visible again.
                            if (reportCell.IsFocused)
                            {
                                if (this.IsKeyboardFocusWithin)
                                {
                                    if (FocusManager.GetFocusedElement(this.ReportGrid) != frameworkElement)
                                    {
                                        FocusManager.SetFocusedElement(this.ReportGrid, frameworkElement);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // At this point, all the elements that can be recycled have been recycled.  The remaining elements on the canvas that
            // are not associated with a virtual report element need to be removed.
            for (int index = 0; index < this.Children.Count;)
            {
                UIElement uiElement = this.Children[index];
                if (DynamicReport.GetCell(uiElement) == ReportCell.Empty)
                {
                    this.Children.RemoveAt(index);
                }
                else
                {
                    index++;
                }
            }

            // The base class takes care of the hard work of measuring the visible user interface elements on the canvas.
            return(base.MeasureOverride(constraint));
        }
コード例 #4
0
        /// <summary>
        /// Handles the mouse button being pressed.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            // This state variable will control how the 'Mouse Move' and 'Mouse Up' event handlers interpret the user action.  The
            // 'selectedColumn' field is used as the starting point for any drag-and-drop type of operation.
            this.mouseState = MouseState.ButtonDown;

            // Evaluate the state of the keyboard.  Key combinations involving the shift and control keys will alter the areas that
            // are selected.
            bool isShiftKeyPressed   = ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift);
            bool isControlKeyPressed = ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control);

            // The mouse selected with the mouse button is used as an anchor point unless the shift key is pressed.  The anchor
            // point allows for ranges of columns to be selected.  Everything between the anchor and the currently selected column
            // will be selected when the shift key is down.  This is modeled after the Excel extended range selection keys.
            Point mouseDownLocation = e.GetPosition(this);

            // Do not clear the select cells in the Report Grid if the right mouse button is pressed.
            if (e.RightButton != MouseButtonState.Pressed)
            {
                //Since the user is selecting a cell, it will invalidate any row selection that there may be.
                foreach (List <ReportRow> block in this.ReportGrid.SelectedRowBlocks)
                {
                    foreach (ReportRow row in block)
                    {
                        foreach (ReportCell cell in row.Cells)
                        {
                            cell.IsSelected = false;
                        }
                    }
                }
                this.ReportGrid.SelectedRowBlocks.Clear();
                this.ReportGrid.SelectedRowHeaderBlocks.Clear();
            }


            if (!isShiftKeyPressed)
            {
                IInputElement    iInputElement    = this.InputHitTest(mouseDownLocation);
                DependencyObject dependencyObject = iInputElement as DependencyObject;
                while (dependencyObject != null)
                {
                    ReportCell reportCell = DynamicReport.GetCell(dependencyObject);
                    if (reportCell != null)
                    {
                        Keyboard.Focus(dependencyObject as IInputElement);
                        currentSelectedCell = reportCell;
                        break;
                    }
                    dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
                }
                this.anchorPoint = mouseDownLocation;
            }

            // The shift and control key extend the selection operation in the same way as Microsoft Excel.
            if (isShiftKeyPressed || isControlKeyPressed)
            {
                // When the shift key is pressed during column selection, every column between the last column selected
                // and the current column is selected.
                if (isShiftKeyPressed)
                {
                    // This is an ordered rectangle that encompasses all the selected cells.
                    Rect selectedRectangle = GetSelectionRectangle(mouseDownLocation);
                    SetSelectedRectangle(selectedRectangle);
                }

                // When the control key is pressed a single column is added to the range of columns selected.
                if (isControlKeyPressed)
                {
                    // A point marks the range when the control key is pressed.  This is almost identical to pressing the mouse
                    // button except the previous ranges are not cleared.  This is part of the extended selection algorithm that is
                    // modeled after Excel.
                    Rect selectedRange = new Rect(mouseDownLocation, new Size(0, 0));

                    // This removes any previous instance of this column in the selection.
                    foreach (Rect existingRange in this.selectedRanges)
                    {
                        if (selectedRange == existingRange)
                        {
                            this.selectedRanges.Remove(existingRange);
                            break;
                        }
                    }

                    // The new range becomes the starting point for any further extended selection operations.
                    this.selectedRanges.Add(selectedRange);
                }

                // This instructs the event handlers how the mouse movement is to be interpreted.
                this.mouseState = MouseState.Selecting;
            }
            else
            {
                // Evaluate if we need to be able to determine the when we right click within the currently selected range then do not clear the selectedRanges.
                if (!((e.RightButton == MouseButtonState.Pressed) && (CurrentSelectedCell != null) && (CurrentSelectedCell.IsSelected)))                 //MIGHT try this --> if (reportCell.Rect.IntersectsWith(selectedRange))
                {
                    // Clear the selected ranges as we do not have a right mouse button pressed with the currently Selected ranges.

                    // A simple selection that doesn't involve the modifier keys will clear out any previously selected ranges.
                    this.selectedRanges.Clear();
                    // The column is added at the start of a new range of selected columns.
                    this.selectedRanges.Add(new Rect(mouseDownLocation, new Size(0, 0)));
                }
            }


            // Evaluate if we need to be able to determine the when we right click within the currently selected range then do not clear the selectedRanges.
            if (!((e.RightButton == MouseButtonState.Pressed) && (CurrentSelectedCell != null) && (CurrentSelectedCell.IsSelected)))             //MIGHT try this --> if (reportCell.Rect.IntersectsWith(selectedRange))
            {
                // This will select all the columns in the selected ranges of columns and remove the selection from all the rest of the cells.
                SelectCells();
            }
        }
コード例 #5
0
 /// <summary>
 /// Get the generated framework element for a cell.
 /// </summary>
 /// <param name="cell">The cell.</param>
 /// <returns>The framework element.</returns>
 public FrameworkElement GetFrameworkElement(ReportCell cell)
 {
     return(this.elementTable[cell]);
 }