Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately the <em>IValueConverter</em> approach will not always work; if the children are added to <em>StackPanel</em> statically, for example, the child collection will be empty at the time of binding (so I discovered). The simplest solution is to create a custom panel:</p> <pre><code>public class ConstrainedStackPanel : StackPanel { public ConstrainedStackPanel() { } protected override Size MeasureOverride(Size constraint) { foreach (var item in this.Children) { // FrameworkElement has the Width property we care about. FrameworkElement element = item as FrameworkElement; if (element != null) constraint.Width = Math.Min(element.Width, constraint.Width); } return base.MeasureOverride(constraint); } protected override Size ArrangeOverride(Size arrangeSize) { foreach (var item in this.Children) { // FrameworkElement has the Width property we care about. FrameworkElement element = item as FrameworkElement; if (element != null) arrangeSize.Width = Math.Min(element.Width, arrangeSize.Width); } return base.ArrangeOverride(arrangeSize); } } </code></pre> <p>You can use the panel as illustrated by the following XAML:</p> <pre><code>&lt;StackPanel Margin="5"&gt; &lt;TextBlock Text="StackPanel:" FontWeight="Bold" /&gt; &lt;StackPanel x:Name="panelA"&gt; &lt;Button Width="100" Content="100" /&gt; &lt;Button Width="200" Content="200" /&gt; &lt;Button Width="300" Content="300" /&gt; &lt;Button Width="400" Content="400" /&gt; &lt;/StackPanel&gt; &lt;TextBlock Text="ConstrainedStackPanel:" FontWeight="Bold" Margin="0,10,0,0" /&gt; &lt;l:ConstrainedStackPanel x:Name="panelB"&gt; &lt;Button Width="100" Content="100" /&gt; &lt;Button Width="200" Content="200" /&gt; &lt;Button Width="300" Content="300" /&gt; &lt;Button Width="400" Content="400" /&gt; &lt;/l:ConstrainedStackPanel&gt; &lt;/StackPanel&gt; </code></pre> <p>Which will render something like the following:</p> <h2><img src="https://farm4.static.flickr.com/3133/3118591068_b1edcce6c0_m.jpg" alt="ScreenShot"></h2> <p>I hope this helps.</p>
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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