/snippets/VS2010/WpfHint/UIBuilding/ParamPanel.cs

http://github.com/xxVisorxx/nemerle · C# · 122 lines · 102 code · 19 blank · 1 comment · 6 complexity · 7d16f3757d516c5f7a9f23217d05ad5e MD5 · raw file

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace WpfHint.UIBuilding
  5. {
  6. internal class ParamPanel : Panel
  7. {
  8. public static bool GetNameColumn(DependencyObject obj)
  9. {
  10. return (bool)obj.GetValue(NameColumnProperty);
  11. }
  12. public static void SetNameColumn(DependencyObject obj, bool value)
  13. {
  14. obj.SetValue(NameColumnProperty, value);
  15. }
  16. public static readonly DependencyProperty NameColumnProperty =
  17. DependencyProperty.RegisterAttached(
  18. "NameColumn", typeof(bool), typeof(ParamPanel),
  19. new FrameworkPropertyMetadata(false));
  20. public static readonly DependencyProperty WrapProperty =
  21. HintDecorator.WrapProperty.AddOwner(
  22. typeof(ParamPanel),
  23. new FrameworkPropertyMetadata((d, e) => ((UIElement)d).InvalidateMeasure()));
  24. #region Layout
  25. private const double tabSpace = 20;
  26. private double nameColumnWidth;
  27. protected override Size MeasureOverride(Size availableSize)
  28. {
  29. var panelSize = new Size();
  30. if (!HintDecorator.GetWrap(this))
  31. {
  32. foreach (UIElement child in base.InternalChildren)
  33. {
  34. child.Measure(availableSize);
  35. var desiredSize = child.DesiredSize;
  36. panelSize.Width += desiredSize.Width;
  37. panelSize.Height = Math.Max(panelSize.Height, desiredSize.Height);
  38. }
  39. }
  40. else
  41. {
  42. nameColumnWidth = 0.0;
  43. var typeColumnWidth = 0.0;
  44. var rowHeight = 0.0;
  45. foreach (UIElement child in base.InternalChildren)
  46. {
  47. child.Measure(availableSize);
  48. var ds = child.DesiredSize;
  49. if (GetNameColumn(child))
  50. {
  51. panelSize.Height += rowHeight;
  52. // tab sapce
  53. ds.Width += tabSpace;
  54. if (ds.Width > nameColumnWidth) nameColumnWidth = ds.Width;
  55. rowHeight = ds.Height;
  56. }
  57. else
  58. {
  59. if (ds.Width > typeColumnWidth) typeColumnWidth = ds.Width;
  60. rowHeight = Math.Max(rowHeight, ds.Height);
  61. }
  62. }
  63. panelSize.Height += rowHeight;
  64. panelSize.Width = nameColumnWidth + typeColumnWidth;
  65. }
  66. return panelSize;
  67. }
  68. protected override Size ArrangeOverride(Size finalSize)
  69. {
  70. if (!HintDecorator.GetWrap(this))
  71. {
  72. var pt = new Point();
  73. foreach (UIElement child in base.InternalChildren)
  74. {
  75. var size = child.DesiredSize;
  76. child.Arrange(new Rect(pt, size));
  77. pt.X += size.Width;
  78. }
  79. }
  80. else
  81. {
  82. var ht = 0.0;
  83. var y = 0.0;
  84. foreach (UIElement child in base.InternalChildren)
  85. {
  86. var size = child.DesiredSize;
  87. if (GetNameColumn(child))
  88. {
  89. child.Arrange(new Rect(tabSpace, y, nameColumnWidth - tabSpace, size.Height));
  90. ht = size.Height;
  91. }
  92. else
  93. {
  94. child.Arrange(new Rect(nameColumnWidth, y, size.Width, size.Height));
  95. y += Math.Max(ht, size.Height);
  96. }
  97. }
  98. }
  99. return finalSize;
  100. }
  101. #endregion
  102. }
  103. }