Beispiel #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();
        }
Beispiel #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
                }
            }
        }
Beispiel #3
0
        private void InitColumns()
        {
            if (!(ListView.View is GridView gridView))
            {
                return;
            }

            foreach (var gridViewColumn in gridView.Columns)
            {
                if (!RangeColumn.IsRangeColumn(gridViewColumn))
                {
                    continue;
                }

                var minWidth = RangeColumn.GetRangeMinWidth(gridViewColumn);
                var maxWidth = RangeColumn.GetRangeMaxWidth(gridViewColumn);
                if (!minWidth.HasValue && !maxWidth.HasValue)
                {
                    continue;
                }

                var columnHeader = FindColumnHeader(ListView, gridViewColumn);
                if (columnHeader is null)
                {
                    continue;
                }

                var actualWidth = columnHeader.ActualWidth;
                if (minWidth.HasValue)
                {
                    columnHeader.SetCurrentValue(FrameworkElement.MinWidthProperty, minWidth.Value);
                    if (!double.IsInfinity(actualWidth) && actualWidth < columnHeader.MinWidth)
                    {
                        gridViewColumn.SetCurrentValue(GridViewColumn.WidthProperty, columnHeader.MinWidth);
                    }
                }
                if (maxWidth.HasValue)
                {
                    columnHeader.SetCurrentValue(FrameworkElement.MaxWidthProperty, maxWidth.Value);
                    if (!double.IsInfinity(actualWidth) && actualWidth > columnHeader.MaxWidth)
                    {
                        gridViewColumn.SetCurrentValue(GridViewColumn.WidthProperty, columnHeader.MaxWidth);
                    }
                }
            }
        }
Beispiel #4
0
        private bool IsFillColumn(GridViewColumn?gridViewColumn)
        {
            if (gridViewColumn is null)
            {
                return(false);
            }

            var view = ListView.View as GridView;

            if (view is null || view.Columns.Count == 0)
            {
                return(false);
            }

            var isFillColumn = RangeColumn.GetRangeIsFillColumn(gridViewColumn);

            return(isFillColumn.HasValue && isFillColumn.Value);
        }
Beispiel #5
0
        // returns the delta
        private double SetRangeColumnToBounds(GridViewColumn gridViewColumn)
        {
            var startWidth = gridViewColumn.Width;

            var minWidth = RangeColumn.GetRangeMinWidth(gridViewColumn);
            var maxWidth = RangeColumn.GetRangeMaxWidth(gridViewColumn);

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

            if (minWidth.HasValue && gridViewColumn.Width < minWidth.Value)
            {
                gridViewColumn.SetCurrentValue(GridViewColumn.WidthProperty, minWidth.Value);
            }
            else if (maxWidth.HasValue && gridViewColumn.Width > maxWidth.Value)
            {
                gridViewColumn.SetCurrentValue(GridViewColumn.WidthProperty, maxWidth.Value);
            }

            return(gridViewColumn.Width - startWidth);
        }
Beispiel #6
0
        private void ResizeColumns()
        {
            var view = ListView.View as GridView;

            if (view is null || view.Columns.Count == 0)
            {
                return;
            }

            var actualWidth = double.PositiveInfinity;

            if (_scrollViewer != null)
            {
                actualWidth = _scrollViewer.ViewportWidth;
            }
            if (double.IsInfinity(actualWidth))
            {
                actualWidth = ListView.ActualWidth;
            }
            if (double.IsInfinity(actualWidth) || actualWidth <= 0)
            {
                return;
            }

            double resizeableRegionCount = 0;
            double otherColumnsWidth     = 0;

            // determine column sizes
            foreach (var gridViewColumn in view.Columns)
            {
                if (ProportionalColumn.IsProportionalColumn(gridViewColumn))
                {
                    var proportionalWidth = ProportionalColumn.GetProportionalWidth(gridViewColumn);
                    if (proportionalWidth != null)
                    {
                        resizeableRegionCount += proportionalWidth.Value;
                    }
                }
                else
                {
                    otherColumnsWidth += gridViewColumn.ActualWidth;
                }
            }

            if (resizeableRegionCount <= 0)
            {
                // no proportional columns present: commit the regulation to the scroll viewer
                if (_scrollViewer != null)
                {
                    _scrollViewer.SetCurrentValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);
                }

                // search the first fill column
                GridViewColumn?fillColumn = null;
                for (var i = 0; i < view.Columns.Count; i++)
                {
                    var gridViewColumn = view.Columns[i];
                    if (IsFillColumn(gridViewColumn))
                    {
                        fillColumn = gridViewColumn;
                        break;
                    }
                }

                if (fillColumn != null)
                {
                    var otherColumnsWithoutFillWidth = otherColumnsWidth - fillColumn.ActualWidth;
                    var fillWidth = actualWidth - otherColumnsWithoutFillWidth;
                    if (fillWidth > 0)
                    {
                        var minWidth = RangeColumn.GetRangeMinWidth(fillColumn);
                        var maxWidth = RangeColumn.GetRangeMaxWidth(fillColumn);

                        var setWidth = !(minWidth.HasValue && fillWidth < minWidth.Value);
                        if (maxWidth.HasValue && fillWidth > maxWidth.Value)
                        {
                            setWidth = false;
                        }
                        if (setWidth)
                        {
                            if (_scrollViewer != null)
                            {
                                _scrollViewer.SetCurrentValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
                            }
                            fillColumn.SetCurrentValue(GridViewColumn.WidthProperty, fillWidth);
                        }
                    }
                }
                return;
            }

            var resizeableColumnsWidth = actualWidth - otherColumnsWidth;

            if (resizeableColumnsWidth <= 0)
            {
                return;
            }

            // resize columns
            var resizeableRegionWidth = resizeableColumnsWidth / resizeableRegionCount;

            foreach (var gridViewColumn in view.Columns)
            {
                if (ProportionalColumn.IsProportionalColumn(gridViewColumn))
                {
                    var proportionalWidth = ProportionalColumn.GetProportionalWidth(gridViewColumn);
                    if (proportionalWidth != null)
                    {
                        gridViewColumn.SetCurrentValue(GridViewColumn.WidthProperty, proportionalWidth.Value * resizeableRegionWidth);
                    }
                }
            }
        }