AutoSize colums for listview
It’s better when you have a ListView can have many columns can be autosized with this code it’s possible
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
public class ListViewHelper { #region MinimumWidth (ATTACHED) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static double GetMinimumWidth(DependencyObject obj) { return (double)obj.GetValue(MinimumWidthProperty); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static void SetMinimumWidth(DependencyObject obj, double value) { obj.SetValue(MinimumWidthProperty, value); } // Using a DependencyProperty as the backing store for MinimumWidth. This enables animation, styling, binding, etc... public static readonly DependencyProperty MinimumWidthProperty = DependencyProperty.RegisterAttached( "MinimumWidth", typeof(double), typeof(ListViewHelper), new PropertyMetadata(double.NaN) ); #endregion #region ExtendWidth (ATTACHED) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static bool GetExtendWidth(DependencyObject obj) { return (bool)obj.GetValue(ExtendWidthProperty); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static void SetExtendWidth(DependencyObject obj, bool value) { obj.SetValue(ExtendWidthProperty, value); } // Using a DependencyProperty as the backing store for ExtendWidth. This enables animation, styling, binding, etc... public static readonly DependencyProperty ExtendWidthProperty = DependencyProperty.RegisterAttached( "ExtendWidth", typeof(bool), typeof(ListViewHelper), new PropertyMetadata(false) ); #endregion #region UpdateColumnWidthExtended (ATTACHED) public static bool GetUpdateColumnWidthExtended(DependencyObject obj) { return (bool)obj.GetValue(UpdateColumnWidthExtendedProperty); } public static void SetUpdateColumnWidthExtended(DependencyObject obj, bool value) { obj.SetValue(UpdateColumnWidthExtendedProperty, value); } public static readonly DependencyProperty UpdateColumnWidthExtendedProperty = DependencyProperty.RegisterAttached("UpdateColumnWidthExtended", typeof(bool), typeof(ListViewHelper), new FrameworkPropertyMetadata(false, (ss, ee) => { ListView lv = ss as ListView; lv.Loaded += (s, e) => { ScrollContentPresenter presenter = GetChildOfType<ScrollContentPresenter>(lv); GridViewHeaderRowPresenter rowPresenter = GetChildOfType<GridViewHeaderRowPresenter>(lv); //double decal = (presenter.ActualWidth - rowPresenter.ActualWidth) + rowPresenter.Margin.Left; double decal = (presenter.ActualWidth - Math.Min(presenter.ActualWidth, rowPresenter.ActualWidth)) + rowPresenter.Margin.Left; SizeChangedEventHandler sizeChanged = (notUsed, nullArgs) => { //if (cpt++ > 5) return; GridView gv = (lv.View as GridView); double availableSpace = presenter.ActualWidth - decal; double remainingSpace = availableSpace; int extendedWidthColumnCount = 0; foreach (var column in gv.Columns) { if (!GetExtendWidth(column)) { remainingSpace -= column.ActualWidth; } else { extendedWidthColumnCount++; } } double totalSize = 0; foreach (var column in gv.Columns) { if (GetExtendWidth(column)) { double minimumWidth = double.IsNaN(GetMinimumWidth(column)) ? 0.0 : GetMinimumWidth(column); double value = Math.Max(minimumWidth, remainingSpace / extendedWidthColumnCount); column.Width = value > 0 ? value : 0; } totalSize += column.Width; } #region disabled //Console.WriteLine("total = " + (totalSize + 20) + " availmable = " + availableSpace); //if (totalSize + 20 >= availableSpace) //{ // ScrollViewer.SetHorizontalScrollBarVisibility(lv, ScrollBarVisibility.Visible); //} //else //{ // ScrollViewer.SetHorizontalScrollBarVisibility(lv, ScrollBarVisibility.Hidden); //} #endregion }; if (presenter != null) { presenter.SizeChanged += sizeChanged; sizeChanged(presenter, null); } }; })); #endregion #region PRIVATE STATIC private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject { if (depObj == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var result = (child as T) ?? GetChildOfType<T>(child); if (result != null) return result; } return null; } #endregion } |
and usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<ListView ItemsSource="{Binding Personnes}" SelectedItem="{Binding SelectedPersonne}" local:ListViewHelper.UpdateColumnWidthExtended="true" Grid.Row="1" Grid.Column="1"> <ListView.View> <GridView> <GridViewColumn Header="nom" DisplayMemberBinding="{Binding Nom}" local:ListViewHelper.ExtendWidth="true" local:ListViewHelper.MinimumWidth="100" /> <GridViewColumn Header="Prenom" DisplayMemberBinding="{Binding Prenom}" local:ListViewHelper.ExtendWidth="true" /> <GridViewColumn Header="Age" local:ListViewHelper.ExtendWidth="true" DisplayMemberBinding="{Binding Age}" /> </GridView> </ListView.View> </ListView> |
for information
- use local:ListViewHelper.UpdateColumnWidthExtended= »true » on ListView for define support resizing columns
- use local:ListViewHelper.ExtendWidth= »true » on each colums who is resized automaticly (like * on grid)
- use local:ListViewHelper.MinimumWidth= »100″ on colum when you want to have a minimum width
this is not tested with virtualized ListView (but when i nedded i make this)
see result :
and the same code with behaviror (for fun)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
class ListViewUpdateColBehavior : Behavior<ListView> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += (s, e) => { ScrollContentPresenter presenter = GetChildOfType<ScrollContentPresenter>(AssociatedObject); GridViewHeaderRowPresenter rowPresenter = GetChildOfType<GridViewHeaderRowPresenter>(AssociatedObject); double decal = (presenter.ActualWidth - rowPresenter.ActualWidth) + rowPresenter.Margin.Left; SizeChangedEventHandler sizeChanged = (notUsed, nullArgs) => { //if (cpt++ > 5) return; GridView gv = (AssociatedObject.View as GridView); double availableSpace = presenter.ActualWidth - decal; double remainingSpace = availableSpace; int extendedWidthColumnCount = 0; foreach (var column in gv.Columns) { if (!GetIsExtendWidth(column)) { remainingSpace -= column.ActualWidth; } else { extendedWidthColumnCount++; } } double totalSize = 0; foreach (var column in gv.Columns) { if (GetIsExtendWidth(column)) { double minimumWidth = double.IsNaN(GetMinimumWidth(column)) ? 0.0 : GetMinimumWidth(column); double value = Math.Max(minimumWidth, remainingSpace / extendedWidthColumnCount); column.Width = value > 0 ? value : 0; } totalSize += column.Width; } #region disabled //Console.WriteLine("total = " + (totalSize + 20) + " availmable = " + availableSpace); //if (totalSize + 20 >= availableSpace) //{ // ScrollViewer.SetHorizontalScrollBarVisibility(lv, ScrollBarVisibility.Visible); //} //else //{ // ScrollViewer.SetHorizontalScrollBarVisibility(lv, ScrollBarVisibility.Hidden); //} #endregion }; if (presenter != null) { presenter.SizeChanged += sizeChanged; sizeChanged(presenter, null); } }; } #region IsExtendWidth public static bool GetIsExtendWidth(DependencyObject obj) { return (bool)obj.GetValue(IsExtendWidthProperty); } public static void SetIsExtendWidth(DependencyObject obj, bool value) { obj.SetValue(IsExtendWidthProperty, value); } public static readonly DependencyProperty IsExtendWidthProperty = DependencyProperty.RegisterAttached("IsExtendWidth", typeof(bool), typeof(ListViewUpdateColBehavior), new PropertyMetadata(false )); #endregion #region MinimumWidth public static double GetMinimumWidth(DependencyObject obj) { return (double)obj.GetValue(MinimumWidthProperty); } public static void SetMinimumWidth(DependencyObject obj, double value) { obj.SetValue(MinimumWidthProperty, value); } public static readonly DependencyProperty MinimumWidthProperty = DependencyProperty.RegisterAttached("MinimumWidth", typeof(double), typeof(ListViewUpdateColBehavior), new PropertyMetadata(double.NaN )); #endregion #region PRIVATE STATIC private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject { if (depObj == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var result = (child as T) ?? GetChildOfType<T>(child); if (result != null) return result; } return null; } #endregion } |
and use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<ListView ItemsSource="{Binding Personnes}" SelectedItem="{Binding SelectedPersonne}" Grid.Row="3" Grid.Column="1"> <b:Interaction.Behaviors> <local:ListViewUpdateColBehavior /> </b:Interaction.Behaviors> <ListView.View> <GridView> <GridViewColumn Header="nom" local:ListViewUpdateColBehavior.IsExtendWidth="true" local:ListViewUpdateColBehavior.MinimumWidth="100" DisplayMemberBinding="{Binding Nom}" /> <GridViewColumn Header="Prenom" local:ListViewUpdateColBehavior.IsExtendWidth="true" DisplayMemberBinding="{Binding Prenom}" /> <GridViewColumn Header="Age" local:ListViewUpdateColBehavior.IsExtendWidth="true" DisplayMemberBinding="{Binding Age}" /> </GridView> </ListView.View> </ListView> |
Categories: Non classé
Commentaires récents