Advertisement
ffMathy

C# Windows 8 Metro FlowPanel

Sep 25th, 2012
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1.     public class WrapPanel : Panel
  2.     {
  3.         public Orientation Orientation
  4.         {
  5.             get { return (Orientation)GetValue(OrientationProperty); }
  6.             set { SetValue(OrientationProperty, value); }
  7.         }
  8.  
  9.         public static readonly DependencyProperty OrientationProperty =
  10.             DependencyProperty.Register("Orientation", typeof(Orientation), typeof(WrapPanel), null);
  11.  
  12.         public WrapPanel()
  13.         {
  14.             // default orientation
  15.             Orientation = Orientation.Horizontal;
  16.         }
  17.  
  18.         protected override Size MeasureOverride(Size availableSize)
  19.         {
  20.  
  21.             Point point = new Point(0, 0);
  22.  
  23.             int i = 0;
  24.  
  25.             if (Orientation == Orientation.Horizontal)
  26.             {
  27.                 double largestSubHeight = 0.0;
  28.                 double finalHeight = 0.0;
  29.  
  30.                 foreach (UIElement child in Children)
  31.                 {
  32.                     child.Measure(new Size(availableSize.Width, availableSize.Height));
  33.  
  34.                     if (child.DesiredSize.Height > largestSubHeight)
  35.                         largestSubHeight = child.DesiredSize.Height;
  36.  
  37.                     point.X = point.X + child.DesiredSize.Width;
  38.  
  39.                     if ((i + 1) < Children.Count)
  40.                     {
  41.                         if ((point.X + Children[i + 1].DesiredSize.Width) > availableSize.Width)
  42.                         {
  43.                             point.X = 0;
  44.                             point.Y = point.Y + largestSubHeight;
  45.                             finalHeight += largestSubHeight;
  46.                             largestSubHeight = 0.0;
  47.                         }
  48.                     }
  49.  
  50.                     i++;
  51.                 }
  52.  
  53.                 return new Size(ActualWidth, finalHeight);
  54.             }
  55.             else
  56.             {
  57.                 double largestSubWidth = 0.0;
  58.                 double finalWidth = 0.0;
  59.  
  60.                 foreach (UIElement child in Children)
  61.                 {
  62.                     child.Measure(new Size(availableSize.Width, availableSize.Height));
  63.  
  64.                     if (child.DesiredSize.Width > largestSubWidth)
  65.                     {
  66.                         largestSubWidth = child.DesiredSize.Width;
  67.                     }
  68.  
  69.                     point.Y = point.Y + child.DesiredSize.Height;
  70.  
  71.                     if ((i + 1) < Children.Count)
  72.                     {
  73.                         if ((point.Y + Children[i + 1].DesiredSize.Height) > availableSize.Height)
  74.                         {
  75.                             point.Y = 0;
  76.                             point.X = point.X + largestSubWidth;
  77.                             finalWidth += largestSubWidth;
  78.                             largestSubWidth = 0.0;
  79.                         }
  80.                     }
  81.  
  82.                     i++;
  83.                 }
  84.  
  85.                 return new Size(finalWidth, ActualHeight);
  86.  
  87.             }
  88.         }
  89.  
  90.         protected override Size ArrangeOverride(Size finalSize)
  91.         {
  92.             Point point = new Point(0, 0);
  93.  
  94.             int i = 0;
  95.  
  96.             if (Orientation == Orientation.Horizontal)
  97.             {
  98.                 double largestSubHeight = 0.0;
  99.                 double finalHeight = 0.0;
  100.  
  101.                 foreach (UIElement child in Children)
  102.                 {
  103.                     child.Arrange(new Rect(point, new Point(point.X + child.DesiredSize.Width, point.Y + child.DesiredSize.Height)));
  104.  
  105.                     if (child.DesiredSize.Height > largestSubHeight)
  106.                         largestSubHeight = child.DesiredSize.Height;
  107.  
  108.                     point.X = point.X + child.DesiredSize.Width;
  109.  
  110.                     if ((i + 1) < Children.Count)
  111.                     {
  112.                         if ((point.X + Children[i + 1].DesiredSize.Width) > finalSize.Width)
  113.                         {
  114.                             point.X = 0;
  115.                             point.Y = point.Y + largestSubHeight;
  116.                             finalHeight += largestSubHeight;
  117.                             largestSubHeight = 0.0;
  118.                         }
  119.                     }
  120.  
  121.                     i++;
  122.                 }
  123.  
  124.                 return Size.Empty;
  125.             }
  126.             else
  127.             {
  128.                 double largestSubWidth = 0.0;
  129.                 double finalWidth = 0.0;
  130.  
  131.                 foreach (UIElement child in Children)
  132.                 {
  133.                     child.Arrange(new Rect(point, new Point(point.X + child.DesiredSize.Width, point.Y + child.DesiredSize.Height)));
  134.  
  135.                     if (child.DesiredSize.Width > largestSubWidth)
  136.                     {
  137.                         largestSubWidth = child.DesiredSize.Width;
  138.                     }
  139.  
  140.                     point.Y = point.Y + child.DesiredSize.Height;
  141.  
  142.                     if ((i + 1) < Children.Count)
  143.                     {
  144.                         if ((point.Y + Children[i + 1].DesiredSize.Height) > finalSize.Height)
  145.                         {
  146.                             point.Y = 0;
  147.                             point.X = point.X + largestSubWidth;
  148.                             finalWidth += largestSubWidth;
  149.                             largestSubWidth = 0.0;
  150.                         }
  151.                     }
  152.  
  153.                     i++;
  154.                 }
  155.  
  156.             }
  157.  
  158.             return base.ArrangeOverride(finalSize);
  159.         }
  160.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement