static void AdjustColumns(ListView listView) { if (ListViewManager.GetAutoSizeColumns(listView)) { var gv = Ensure.That(listView.View as GridView) .WithMessage("This behavior can be attached only to ListView(s) whose view is a GridView.") .IsNotNull() .GetValue(); var columnsToStretch = gv.Columns.Where(c => GridViewColumnManager.GetFill(c)); if (columnsToStretch.Any()) { var subtract = .0d; var sv = VisualTreeCrawler.FindChild <ScrollViewer>(listView); if (sv != null && sv.ComputedVerticalScrollBarVisibility == Visibility.Visible) { var sb = VisualTreeCrawler.FindChild <ScrollBar>(sv, child => child.Orientation == Orientation.Vertical); if (sb != null) { subtract = sb.Width; } } var nonStretchedColumnsActualWidth = gv.Columns .Where(c => !GridViewColumnManager.GetFill(c)) .Aggregate(0d, (w, c) => { w += c.ActualWidth; return(w); }); var availableWidth = listView.ActualWidth - subtract - nonStretchedColumnsActualWidth; if (availableWidth <= 0) { logger.Warning ( "Columns cannot be auto sized, available size is invalid: {0}", availableWidth ); } if (availableWidth > 0) { var columnsAvarageWidth = availableWidth / columnsToStretch.Count(); foreach (var column in columnsToStretch) { column.Width = columnsAvarageWidth; } } } } }
private static void OnSortCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!DesignTimeHelper.GetIsInDesignMode()) { var listView = Ensure.That(d as ListView) .Named("d") .WithMessage("This behavior can be attached only to ListView(s).") .IsNotNull() .GetValue(); if (!ListViewManager.GetIsLoadEventAttached(listView)) { listView.Loaded += onLoaded; ListViewManager.SetIsLoadEventAttached(listView, true); } } }