private void ResetColumnWidths() { if (!resetColumnWidthsPending) { resetColumnWidthsPending = true; Dispatcher.CurrentDispatcher.BeginInvoke( (Action)(() => { resetColumnWidthsPending = false; GridView view = listView.View as GridView; if (view != null) { listView.UpdateLayout(); // Prevent flickering on expanding columns (not collapsing though) foreach (GridViewColumn gridViewColumn in view.Columns) { bool?isFillColumn = RangeColumn.GetRangeIsFillColumn(gridViewColumn); if (!isFillColumn.HasValue || !isFillColumn.Value) { if (double.IsNaN(gridViewColumn.Width)) { gridViewColumn.Width = gridViewColumn.ActualWidth; gridViewColumn.Width = double.NaN; } } } } Dispatcher.CurrentDispatcher.BeginInvoke((Action)Refresh, DispatcherPriority.Render); }), DispatcherPriority.Render); } }
private void GridColumnWidthChanged(object sender, EventArgs args) { if (!loaded) { return; } GridViewColumn gridViewColumn = sender as 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 != null && 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(); }
private void ThumbPreviewMouseMove(object sender, MouseEventArgs args) { Thumb thumb = sender as Thumb; if (thumb == null) { return; } GridViewColumn gridViewColumn = FindParentColumn(thumb); if (gridViewColumn == null) { return; } // suppress column resizing for proportional, fixed and range fill columns if (ProportionalColumn.IsProportionalColumn(gridViewColumn) || FixedColumn.IsFixedColumn(gridViewColumn) || IsFillColumn(gridViewColumn)) { thumb.Cursor = null; return; } // check range column bounds if (thumb.IsMouseCaptured && RangeColumn.IsRangeColumn(gridViewColumn)) { double?minWidth = RangeColumn.GetRangeMinWidth(gridViewColumn); double?maxWidth = RangeColumn.GetRangeMaxWidth(gridViewColumn); if ((minWidth.HasValue && maxWidth.HasValue) && (minWidth > maxWidth)) { return; // invalid case } if (resizeCursor == null) { resizeCursor = thumb.Cursor; // save the resize cursor } if (minWidth.HasValue && gridViewColumn.Width <= minWidth.Value) { thumb.Cursor = Cursors.No; } else if (maxWidth.HasValue && gridViewColumn.Width >= maxWidth.Value) { thumb.Cursor = Cursors.No; } else { thumb.Cursor = resizeCursor; // between valid min/max } } }
private void InitColumns() { GridView view = listView.View as GridView; if (view == null) { return; } foreach (GridViewColumn gridViewColumn in view.Columns) { if (!RangeColumn.IsRangeColumn(gridViewColumn)) { continue; } double?minWidth = RangeColumn.GetRangeMinWidth(gridViewColumn); double?maxWidth = RangeColumn.GetRangeMaxWidth(gridViewColumn); if (!minWidth.HasValue && !maxWidth.HasValue) { continue; } GridViewColumnHeader columnHeader = FindColumnHeader(listView, gridViewColumn); if (columnHeader == null) { continue; } double actualWidth = columnHeader.ActualWidth; if (minWidth.HasValue) { columnHeader.MinWidth = minWidth.Value; if (!double.IsInfinity(actualWidth) && actualWidth < columnHeader.MinWidth) { gridViewColumn.Width = columnHeader.MinWidth; } } if (maxWidth.HasValue) { columnHeader.MaxWidth = maxWidth.Value; if (!double.IsInfinity(actualWidth) && actualWidth > columnHeader.MaxWidth) { gridViewColumn.Width = columnHeader.MaxWidth; } } } }
private bool IsFillColumn(GridViewColumn gridViewColumn) { if (gridViewColumn == null) { return(false); } GridView view = listView.View as GridView; if (view == null || view.Columns.Count == 0) { return(false); } bool?isFillColumn = RangeColumn.GetRangeIsFillColumn(gridViewColumn); return(isFillColumn.HasValue && isFillColumn.Value); }
// returns the delta private double SetRangeColumnToBounds(GridViewColumn gridViewColumn) { double startWidth = gridViewColumn.Width; double?minWidth = RangeColumn.GetRangeMinWidth(gridViewColumn); double?maxWidth = RangeColumn.GetRangeMaxWidth(gridViewColumn); if ((minWidth.HasValue && maxWidth.HasValue) && (minWidth > maxWidth)) { return(0); // invalid case } if (minWidth.HasValue && gridViewColumn.Width < minWidth.Value) { gridViewColumn.Width = minWidth.Value; } else if (maxWidth.HasValue && gridViewColumn.Width > maxWidth.Value) { gridViewColumn.Width = maxWidth.Value; } return(gridViewColumn.Width - startWidth); }
protected virtual void ResizeColumns() { GridView view = listView.View as GridView; if (view == null || view.Columns.Count == 0) { return; } // listview width double actualWidth = double.PositiveInfinity; if (scrollViewer != null) { actualWidth = scrollViewer.ViewportWidth - 4; } if (double.IsInfinity(actualWidth)) { actualWidth = listView.ActualWidth; } if (double.IsInfinity(actualWidth) || actualWidth <= 0) { return; } double resizeableRegionCount = 0; double otherColumnsWidth = 0; // determine column sizes foreach (GridViewColumn gridViewColumn in view.Columns) { if (ProportionalColumn.IsProportionalColumn(gridViewColumn)) { double?proportionalWidth = ProportionalColumn.GetProportionalWidth(gridViewColumn); if (proportionalWidth != null) { resizeableRegionCount += proportionalWidth.Value; } } else { otherColumnsWidth += gridViewColumn.ActualWidth; } } //if (DateTime.Now.Minute >= 4) System.Diagnostics.Debugger.Break(); if (resizeableRegionCount <= 0) { // no proportional columns present: commit the regulation to the scroll viewer if (scrollViewer != null) { scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; } // search the first fill column GridViewColumn fillColumn = null; for (int i = 0; i < view.Columns.Count; i++) { GridViewColumn gridViewColumn = view.Columns[i]; if (IsFillColumn(gridViewColumn)) { fillColumn = gridViewColumn; break; } } if (fillColumn != null) { double otherColumnsWithoutFillWidth = otherColumnsWidth - fillColumn.ActualWidth; double fillWidth = actualWidth - otherColumnsWithoutFillWidth; if (fillWidth > 0) { double?minWidth = RangeColumn.GetRangeMinWidth(fillColumn); double?maxWidth = RangeColumn.GetRangeMaxWidth(fillColumn); bool setWidth = !(minWidth.HasValue && fillWidth < minWidth.Value); if (maxWidth.HasValue && fillWidth > maxWidth.Value) { setWidth = false; } if (setWidth) { if (scrollViewer != null) { scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; } fillColumn.Width = fillWidth; } } else { // There's not enough space for any fill columns to display. // We may need a horizontal scrollbar then. if (scrollViewer != null) { scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; } } } return; } double resizeableColumnsWidth = actualWidth - otherColumnsWidth; if (resizeableColumnsWidth <= 0) { // There's not enough space for any fill columns to display. // We may need a horizontal scrollbar then. if (scrollViewer != null) { scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; } resizeableColumnsWidth = 0; } // resize columns double resizeableRegionWidth = resizeableColumnsWidth / resizeableRegionCount; foreach (GridViewColumn gridViewColumn in view.Columns) { if (ProportionalColumn.IsProportionalColumn(gridViewColumn)) { double?proportionalWidth = ProportionalColumn.GetProportionalWidth(gridViewColumn); if (proportionalWidth != null) { gridViewColumn.Width = proportionalWidth.Value * resizeableRegionWidth; } } } }