You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.8 KiB

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. namespace ICSharpCode.ILSpy.Controls
  24. {
  25. /// <summary>
  26. /// This class adds the AutoWidth property to the WPF ListView.
  27. /// It supports a semi-colon-separated list of values, for each defined cell.
  28. /// Each value can either be a fixed size double, or a percentage.
  29. /// The sizes of columns with a percentage will be calculated from the
  30. /// remaining width (after assigning the fixed sizes).
  31. /// Examples: 50%;25%;25% or 30;100%;50
  32. /// </summary>
  33. public class GridViewColumnAutoSize
  34. {
  35. // This class was copied from ICSharpCode.Core.Presentation.
  36. public static readonly DependencyProperty AutoWidthProperty =
  37. DependencyProperty.RegisterAttached("AutoWidth", typeof(string), typeof(GridViewColumnAutoSize),
  38. new FrameworkPropertyMetadata(null, AutoWidthPropertyChanged));
  39. public static string GetAutoWidth(DependencyObject obj)
  40. {
  41. return (string)obj.GetValue(AutoWidthProperty);
  42. }
  43. public static void SetAutoWidth(DependencyObject obj, string value)
  44. {
  45. obj.SetValue(AutoWidthProperty, value);
  46. }
  47. static void AutoWidthPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  48. {
  49. ListView grid = sender as ListView;
  50. if (grid == null)
  51. return;
  52. grid.SizeChanged += delegate (object listView, SizeChangedEventArgs e) {
  53. ListView lv = listView as ListView;
  54. if (lv == null)
  55. return;
  56. GridView v = lv.View as GridView;
  57. if (v == null)
  58. return;
  59. CalculateSizes(v, GetAutoWidth(lv), e.NewSize.Width);
  60. };
  61. GridView view = grid.View as GridView;
  62. if (view == null)
  63. return;
  64. CalculateSizes(view, args.NewValue as string, grid.ActualWidth);
  65. }
  66. static void CalculateSizes(GridView view, string sizeValue, double fullWidth)
  67. {
  68. string[] sizes = (sizeValue ?? "").Split(';');
  69. if (sizes.Length != view.Columns.Count)
  70. return;
  71. Dictionary<int, Func<double, double>> percentages = new Dictionary<int, Func<double, double>>();
  72. double remainingWidth = fullWidth - 30; // 30 is a good offset for the scrollbar
  73. for (int i = 0; i < view.Columns.Count; i++)
  74. {
  75. var column = view.Columns[i];
  76. double size;
  77. bool isPercentage = !double.TryParse(sizes[i], out size);
  78. if (isPercentage)
  79. {
  80. size = double.Parse(sizes[i].TrimEnd('%', ' '));
  81. percentages.Add(i, w => w * size / 100.0);
  82. }
  83. else
  84. {
  85. column.Width = size;
  86. remainingWidth -= size;
  87. }
  88. }
  89. if (remainingWidth < 0)
  90. return;
  91. foreach (var p in percentages)
  92. {
  93. var column = view.Columns[p.Key];
  94. column.Width = p.Value(remainingWidth);
  95. }
  96. }
  97. }
  98. }