예제 #1
0
        private void GridColumnWidthChanged(object?sender, EventArgs?e)
        {
            if (!_loaded)
            {
                return;
            }
            if (sender is GridViewColumn gridViewColumn)
            {
                // suppress column resizing for proportional and fixed columns
                if (ProportionalColumn.IsProportionalColumn(gridViewColumn) || FixedColumn.IsFixedColumn(gridViewColumn))
                {
                    return;
                }

                // ensure range column within the bounds
                if (RangeColumn.IsRangeColumn(gridViewColumn))
                {
                    // special case: auto column width - maybe conflicts with min/max range
                    if (gridViewColumn.Width.Equals(double.NaN))
                    {
                        _autoSizedColumn = gridViewColumn;
                        return; // handled by the change header size event
                    }

                    // ensure column bounds
                    if (Math.Abs(SetRangeColumnToBounds(gridViewColumn) - 0) > ZeroWidthRange)
                    {
                        return;
                    }
                }
            }

            DoResizeColumns();
        }
예제 #2
0
        private void ThumbPreviewMouseMove(object sender, MouseEventArgs e)
        {
            var thumb = sender as Thumb;

            if (thumb is null)
            {
                return;
            }
            var gridViewColumn = FindParentColumn(thumb);

            if (gridViewColumn is null)
            {
                return;
            }

            // suppress column resizing for proportional, fixed and range fill columns
            if (ProportionalColumn.IsProportionalColumn(gridViewColumn) || FixedColumn.IsFixedColumn(gridViewColumn) || IsFillColumn(gridViewColumn))
            {
                thumb.SetCurrentValue(FrameworkElement.CursorProperty, null);
                return;
            }

            // check range column bounds
            if (thumb.IsMouseCaptured && RangeColumn.IsRangeColumn(gridViewColumn))
            {
                var minWidth = RangeColumn.GetRangeMinWidth(gridViewColumn);
                var maxWidth = RangeColumn.GetRangeMaxWidth(gridViewColumn);

                if (minWidth.HasValue && maxWidth.HasValue && (minWidth > maxWidth))
                {
                    return; // invalid case
                }

                if (_resizeCursor is null)
                {
                    _resizeCursor = thumb.Cursor; // save the resize cursor
                }

                if (minWidth.HasValue && gridViewColumn.Width <= minWidth.Value)
                {
                    thumb.SetCurrentValue(FrameworkElement.CursorProperty, Cursors.No);
                }
                else if (maxWidth.HasValue && gridViewColumn.Width >= maxWidth.Value)
                {
                    thumb.SetCurrentValue(FrameworkElement.CursorProperty, Cursors.No);
                }
                else
                {
                    thumb.SetCurrentValue(FrameworkElement.CursorProperty, _resizeCursor); // between valid min/max
                }
            }
        }
예제 #3
0
        private void ThumbPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!(sender is Thumb thumb))
            {
                return;
            }

            var gridViewColumn = FindParentColumn(thumb);

            // suppress column resizing for proportional, fixed and range fill columns
            if (ProportionalColumn.IsProportionalColumn(gridViewColumn) || FixedColumn.IsFixedColumn(gridViewColumn) || IsFillColumn(gridViewColumn))
            {
                e.Handled = true;
            }
        }
예제 #4
0
        private void RegisterEvents(DependencyObject?start)
        {
            if (start is null)
            {
                return;
            }

            for (var i = 0; i < VisualTreeHelper.GetChildrenCount(start); i++)
            {
                var childVisual = VisualTreeHelper.GetChild(start, i) as Visual;
                if (childVisual is Thumb)
                {
                    var gridViewColumn = FindParentColumn(childVisual);
                    if (gridViewColumn != null)
                    {
                        if (childVisual is Thumb thumb)
                        {
                            if (ProportionalColumn.IsProportionalColumn(gridViewColumn) || FixedColumn.IsFixedColumn(gridViewColumn) || IsFillColumn(gridViewColumn))
                            {
                                thumb.SetCurrentValue(UIElement.IsHitTestVisibleProperty, false);
                            }
                            else
                            {
                                thumb.PreviewMouseMove           += new MouseEventHandler(ThumbPreviewMouseMove);
                                thumb.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(ThumbPreviewMouseLeftButtonDown);
                                DependencyPropertyDescriptor.FromProperty(
                                    GridViewColumn.WidthProperty,
                                    typeof(GridViewColumn)).AddValueChanged(gridViewColumn, GridColumnWidthChanged);
                            }
                        }
                    }
                }
                else if (childVisual is GridViewColumnHeader columnHeader)
                {
                    columnHeader.SizeChanged += new SizeChangedEventHandler(GridColumnHeaderSizeChanged);
                }
                else if (_scrollViewer is null && childVisual is ScrollViewer scrollViewer)
                {
                    _scrollViewer = scrollViewer;
                    _scrollViewer.ScrollChanged += new ScrollChangedEventHandler(ScrollViewerScrollChanged);

                    // assume we do the regulation of the horizontal scrollbar
                    _scrollViewer.SetCurrentValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
                    _scrollViewer.SetCurrentValue(ScrollViewer.VerticalScrollBarVisibilityProperty, VerticalScrollBarVisibility);
                }

                RegisterEvents(childVisual);
            }
        }
예제 #5
0
        private void UnregisterEvents(DependencyObject?start)
        {
            if (start is null)
            {
                return;
            }

            for (var i = 0; i < VisualTreeHelper.GetChildrenCount(start); i++)
            {
                var childVisual = VisualTreeHelper.GetChild(start, i) as Visual;
                if (childVisual is Thumb)
                {
                    var gridViewColumn = FindParentColumn(childVisual);
                    if (gridViewColumn != null)
                    {
                        if (childVisual is Thumb thumb)
                        {
                            if (ProportionalColumn.IsProportionalColumn(gridViewColumn) || FixedColumn.IsFixedColumn(gridViewColumn) || IsFillColumn(gridViewColumn))
                            {
                                thumb.SetCurrentValue(UIElement.IsHitTestVisibleProperty, true);
                            }
                            else
                            {
                                thumb.PreviewMouseMove           -= new MouseEventHandler(ThumbPreviewMouseMove);
                                thumb.PreviewMouseLeftButtonDown -= new MouseButtonEventHandler(ThumbPreviewMouseLeftButtonDown);
                                DependencyPropertyDescriptor.FromProperty(
                                    GridViewColumn.WidthProperty,
                                    typeof(GridViewColumn)).RemoveValueChanged(gridViewColumn, GridColumnWidthChanged);
                            }
                        }
                    }
                }
                else if (childVisual is GridViewColumnHeader columnHeader)
                {
                    columnHeader.SizeChanged -= new SizeChangedEventHandler(GridColumnHeaderSizeChanged);
                }
                else if (_scrollViewer is null && childVisual is ScrollViewer scrollViewer)
                {
                    _scrollViewer = scrollViewer;
                    _scrollViewer.ScrollChanged -= new ScrollChangedEventHandler(ScrollViewerScrollChanged);
                }

                UnregisterEvents(childVisual);
            }
        }