<p>I've created a ListBox to display items in groups, where the groups are wrapped right to left when they can no longer fit within the height of the ListBox's panel. So, the groups would appear similar to this in the listbox, where each group's height is arbitrary (group 1, for instance, is twice as tall as group 2):</p>

<pre><code>[ 1 ][ 3 ][ 5 ]
[   ][ 4 ][ 6 ]
[ 2 ][   ]
</code></pre>

<p>The following XAML works correctly in that it performs the wrapping, and allows the horizontal scroll bar to appear when the items run off the right side of the ListBox.</p>

<pre><code>&lt;ListBox&gt; 
  &lt;ListBox.ItemsPanel&gt; 
    &lt;ItemsPanelTemplate&gt; 
      &lt;StackPanel Orientation="Vertical"/&gt; 
    &lt;/ItemsPanelTemplate&gt; 
  &lt;/ListBox.ItemsPanel&gt; 

  &lt;ListBox.GroupStyle&gt; 
    &lt;ItemsPanelTemplate&gt; 
      &lt;WrapPanel Orientation="Vertical" 
                 Height="{Binding Path=ActualHeight, 
                          RelativeSource={RelativeSource 
                            FindAncestor, 
                            AncestorLevel=1, 
                            AncestorType={x:Type ScrollContentPresenter}}}"/&gt; 
    &lt;/ItemsPanelTemplate&gt; 
  &lt;/ListBox.GroupStyle&gt; 
&lt;/ListBox&gt;
</code></pre>

<p>The problem occurs when a group of items is longer than the height of the WrapPanel. Instead of allowing the vertical scroll bar to appear to view the cutoff item group, the items in that group are simply clipped. I'm assuming that this is a side effect of the Height binding in the WrapPanel - the scrollbar thinks it does not have to enabled.</p>

<p>Is there any way to enable the scrollbar, or another way around this issue that I'm not seeing?</p>

[{'answer_id': 74235, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 0, 'selected': False, 'text': '<p>I would think that you are correct that it has to do with the binding. What happens when you remove the binding? With the binding are you trying to fill up at least the entire height of the list box? If so, consider binding to MinHeight instead, or try using the <code>VerticalAlignment</code> property.</p>\n'}, {'answer_id': 74306, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Thanks for answering, David.</p>\n\n<p>When the binding is <code>removed</code>, no wrapping occurs. The <strong>WrapPanel</strong> puts every group into a single vertical column.</p>\n\n<p>The binding is meant to force the WrapPanel to actually wrap. If no binding is set, the WrapPanel assumes the height is infinite and never wraps.</p>\n\n<p>Binding to <code>MinHeight</code> results in an empty listbox. I can see how the <code>VerticalAlignment</code> property could seem to be a solution, but alignment itself prevents any wrapping from occurring. When binding and alignment are used together, the alignment has no effect on the problem.</p>\n'}, {'answer_id': 74565, 'author': 'Abe Heidebrecht', 'author_id': 9268, 'author_profile': 'https://Stackoverflow.com/users/9268', 'pm_score': 2, 'selected': False, 'text': '<p>By setting the Height property on the WrapPanel to the height of the ScrollContentPresenter, it will never scroll vertically.  However, if you remove that Binding, it will never wrap, since in the layout pass, it has infinite height to layout in.  </p>\n\n<p>I would suggest creating your own panel class to get the behavior you want.  Have a separate dependency property that you can bind the desired height to, so you can use that to calculate the target height in the measure and arrange steps.  If any one child is taller than the desired height, use that child\'s height as the target height to calculate the wrapping.</p>\n\n<p>Here is an example panel to do this:</p>\n\n<pre><code>public class SmartWrapPanel : WrapPanel\n{\n    /// &lt;summary&gt;\n    /// Identifies the DesiredHeight dependency property\n    /// &lt;/summary&gt;\n    public static readonly DependencyProperty DesiredHeightProperty = DependencyProperty.Register(\n        "DesiredHeight",\n        typeof(double),\n        typeof(SmartWrapPanel),\n        new FrameworkPropertyMetadata(Double.NaN, \n            FrameworkPropertyMetadataOptions.AffectsArrange |\n            FrameworkPropertyMetadataOptions.AffectsMeasure));\n\n    /// &lt;summary&gt;\n    /// Gets or sets the height to attempt to be.  If any child is taller than this, will use the child\'s height.\n    /// &lt;/summary&gt;\n    public double DesiredHeight\n    {\n        get { return (double)GetValue(DesiredHeightProperty); }\n        set { SetValue(DesiredHeightProperty, value); }\n    }\n\n    protected override Size MeasureOverride(Size constraint)\n    {\n        Size ret = base.MeasureOverride(constraint);\n        double h = ret.Height;\n\n        if (!Double.IsNaN(DesiredHeight))\n        {\n            h = DesiredHeight;\n            foreach (UIElement child in Children)\n            {\n                if (child.DesiredSize.Height &gt; h)\n                    h = child.DesiredSize.Height;\n            }\n        }\n\n        return new Size(ret.Width, h);\n    }\n\n    protected override System.Windows.Size ArrangeOverride(Size finalSize)\n    {\n        double h = finalSize.Height;\n\n        if (!Double.IsNaN(DesiredHeight))\n        {\n            h = DesiredHeight;\n            foreach (UIElement child in Children)\n            {\n                if (child.DesiredSize.Height &gt; h)\n                    h = child.DesiredSize.Height;\n            }\n        }\n\n        return base.ArrangeOverride(new Size(finalSize.Width, h));\n    }\n}\n</code></pre>\n'}, {'answer_id': 82798, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Here is the slightly modified code - all credit given to Abe Heidebrecht, who previously posted it - that allows both horizontal and vertical scrolling. The only change is that the return value of MeasureOverride needs to be base.MeasureOverride(new Size(ret.width, h)).</p>\n\n<pre><code>// Original code : Abe Heidebrecht\npublic class SmartWrapPanel : WrapPanel\n{\n  /// &lt;summary&gt;\n  /// Identifies the DesiredHeight dependency property\n  /// &lt;/summary&gt;\n  public static readonly DependencyProperty DesiredHeightProperty = DependencyProperty.Register(\n    "DesiredHeight",\n    typeof(double),\n    typeof(SmartWrapPanel),\n    new FrameworkPropertyMetadata(Double.NaN, \n            FrameworkPropertyMetadataOptions.AffectsArrange |\n            FrameworkPropertyMetadataOptions.AffectsMeasure));\n\n  /// &lt;summary&gt;\n  /// Gets or sets the height to attempt to be.  If any child is taller than this, will use the child\'s height.\n  /// &lt;/summary&gt;\n  public double DesiredHeight\n  {\n    get { return (double)GetValue(DesiredHeightProperty); }\n    set { SetValue(DesiredHeightProperty, value); }\n  }\n\n  protected override Size MeasureOverride(Size constraint)\n  {\n    Size ret = base.MeasureOverride(constraint);\n    double h = ret.Height;\n\n    if (!Double.IsNaN(DesiredHeight))\n    {\n      h = DesiredHeight;\n      foreach (UIElement child in Children)\n      {\n        if (child.DesiredSize.Height &gt; h)\n          h = child.DesiredSize.Height;\n      }\n    }\n\n    return base.MeasureOverride(new Size(ret.Width, h));\n  }\n\n  protected override System.Windows.Size ArrangeOverride(Size finalSize)\n  {\n    double h = finalSize.Height;\n\n    if (!Double.IsNaN(DesiredHeight))\n    {\n      h = DesiredHeight;\n      foreach (UIElement child in Children)\n      {\n        if (child.DesiredSize.Height &gt; h)\n          h = child.DesiredSize.Height;\n      }\n    }\n\n    return base.ArrangeOverride(new Size(finalSize.Width, h));\n  }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/74188', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']