Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's an <a href="http://www.codeproject.com/Articles/32629/A-better-panel-for-data-binding-to-a-WrapPanel-in" rel="nofollow">UniformWrapPanel</a> article on codeproject.com, which I modified to ensure the items in a wrap panel have uniform width and fit to the width of the window. You should easily be able to modify this code to have a maximum number of columns. Try changing the code near</p> <pre><code>var itemsPerRow = (int) (totalWidth/ItemWidth); </code></pre> <p>in order to specify the max columns.</p> <p>Here's the code:</p> <pre><code>public enum ItemSize { None, Uniform, UniformStretchToFit } public class UniformWrapPanel : WrapPanel { public static readonly DependencyProperty ItemSizeProperty = DependencyProperty.Register( "ItemSize", typeof (ItemSize), typeof (UniformWrapPanel), new FrameworkPropertyMetadata( default(ItemSize), FrameworkPropertyMetadataOptions.AffectsMeasure, ItemSizeChanged)); private static void ItemSizeChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e) { var uniformWrapPanel = sender as UniformWrapPanel; if (uniformWrapPanel != null) { if (uniformWrapPanel.Orientation == Orientation.Horizontal) { uniformWrapPanel.ItemWidth = double.NaN; } else { uniformWrapPanel.ItemHeight = double.NaN; } } } public ItemSize ItemSize { get { return (ItemSize) GetValue(ItemSizeProperty); } set { SetValue(ItemSizeProperty, value); } } protected override Size MeasureOverride(Size availableSize) { var mode = ItemSize; if (Children.Count &gt; 0 &amp;&amp; mode != ItemSize.None) { bool stretchToFit = mode == ItemSize.UniformStretchToFit; if (Orientation == Orientation.Horizontal) { double totalWidth = availableSize.Width; ItemWidth = 0.0; foreach (UIElement el in Children) { el.Measure(availableSize); Size next = el.DesiredSize; if (!(Double.IsInfinity(next.Width) || Double.IsNaN(next.Width))) { ItemWidth = Math.Max(next.Width, ItemWidth); } } if (stretchToFit) { if (!double.IsNaN(ItemWidth) &amp;&amp; !double.IsInfinity(ItemWidth) &amp;&amp; ItemWidth &gt; 0) { var itemsPerRow = (int) (totalWidth/ItemWidth); if (itemsPerRow &gt; 0) { ItemWidth = totalWidth/itemsPerRow; } } } } else { double totalHeight = availableSize.Height; ItemHeight = 0.0; foreach (UIElement el in Children) { el.Measure(availableSize); Size next = el.DesiredSize; if (!(Double.IsInfinity(next.Height) || Double.IsNaN(next.Height))) { ItemHeight = Math.Max(next.Height, ItemHeight); } } if (stretchToFit) { if (!double.IsNaN(ItemHeight) &amp;&amp; !double.IsInfinity(ItemHeight) &amp;&amp; ItemHeight &gt; 0) { var itemsPerColumn = (int) (totalHeight/ItemHeight); if (itemsPerColumn &gt; 0) { ItemHeight = totalHeight/itemsPerColumn; } } } } } return base.MeasureOverride(availableSize); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload