3 people like it.

WrapPanel

Positions child elements in sequential position from left to right, breaking content to the next line at the edge of the containing box. Tryable at http://tryfsharp.org

WrapPanel control

 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: 
type WrapPanel() =
    inherit Panel()
    override x.MeasureOverride(availableSize:Size) =
        let childMeasure = Size(Double.PositiveInfinity, Double.PositiveInfinity)
        let _, (width,height) =
            x.Children 
            |> Seq.fold (fun ((x, y), (width, height)) child ->
                child.Measure(childMeasure)
                if child.DesiredSize.Width + x > availableSize.Width 
                then (child.DesiredSize.Width, 0.0), (max x width, height + y)
                else (x + child.DesiredSize.Width, y), (width, height)
            ) ((0.0,0.0),(0.0,0.0))
        Size(width, height)
    override x.ArrangeOverride(finalSize:Size) =
        x.Children 
        |> Seq.fold (fun (x,y,height) child ->
            let x,y,height =
                if child.DesiredSize.Width + x > finalSize.Width 
                then 0.0, y + height, 0.0
                else x, y, height
            Rect(x, y, child.DesiredSize.Width, child.DesiredSize.Height)
            |> child.Arrange
            x + child.DesiredSize.Width, y, max height child.DesiredSize.Height
        ) (0.0,0.0,0.0) 
        |> ignore
        finalSize

Sample usage

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
type NumberBox(number:int) as this =
    inherit UserControl(Width=50.0,Height=50.0)
    let block = 
        TextBlock(Text=number.ToString(),
                  Foreground=SolidColorBrush Colors.Blue,
                  HorizontalAlignment=HorizontalAlignment.Center,
                  VerticalAlignment=VerticalAlignment.Center,
                  FontSize=16.0)
    let border = 
        Border(Child=block,
               BorderBrush=SolidColorBrush Colors.Cyan,
               BorderThickness=Thickness(2.0),
               Margin=Thickness(5.0))
    do  this.Content <- border

Run sample in Try F#

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
App.Dispatch (fun () ->
    let panel = WrapPanel(Width=400.0)
    for i = 1 to 50 do NumberBox i |> panel.Children.Add
    App.Console.ClearCanvas()
    let canvas = App.Console.Canvas
    panel |> canvas.Children.Add
    App.Console.CanvasPosition <- CanvasPosition.Right
)
Multiple items
type WrapPanel =
  inherit obj
  new : unit -> WrapPanel
  override ArrangeOverride : finalSize:'a -> 'b
  override MeasureOverride : availableSize:'a -> 'b

Full name: Script.WrapPanel

--------------------
new : unit -> WrapPanel
override WrapPanel.MeasureOverride : availableSize:'a -> 'b

Full name: Script.WrapPanel.MeasureOverride
type Double =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 3 overloads
    static val MinValue : float
    static val MaxValue : float
    static val Epsilon : float
    static val NegativeInfinity : float
    static val PositiveInfinity : float
    ...
  end

Full name: System.Double
field float.PositiveInfinity = Infinity
module Seq

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
Multiple items
type MeasureAttribute =
  inherit Attribute
  new : unit -> MeasureAttribute

Full name: Microsoft.FSharp.Core.MeasureAttribute

--------------------
new : unit -> MeasureAttribute
val max : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.max
override WrapPanel.ArrangeOverride : finalSize:'a -> 'b

Full name: Script.WrapPanel.ArrangeOverride
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Multiple items
type NumberBox =
  inherit obj
  new : number:int -> NumberBox

Full name: Script.NumberBox

--------------------
new : number:int -> NumberBox
val number : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val this : NumberBox
namespace System.Text

More information

Link:http://fssnip.net/aJ
Posted:5 years ago
Author:Phillip Trelford
Tags: silverlight , control