Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm pretty sure the answer is "not easily".</p> <p>Here's an idea that seems to work but it's kind of clunky:</p> <ol> <li><p>Throw your StackPanel into a UserControl (<code>UserControl1</code> in the example below).</p></li> <li><p>Place two copies of this UserControl into a container (a Grid in the example below).</p> <p>a. Align the first copy top/left so that it remains at its default size. This copy is strictly for measurement purposes and should have its Visibility set to Hidden.</p> <p>b. Place the second copy inside a Viewbox. This copy is the one the user will actually see.</p></li> <li><p>Use a MultiBinding with a MultiValueConverter to adjust the width of the second UserControl so that it has the correct aspect ratio <em>before</em> it gets expanded by the Viewbox.</p></li> </ol> <p><strong>Here's the markup:</strong></p> <pre><code>&lt;Grid&gt; &lt;local:UserControl1 x:Name="RawControl" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="Hidden" /&gt; &lt;Viewbox&gt; &lt;local:UserControl1&gt; &lt;local:UserControl1.Width&gt; &lt;MultiBinding Converter="{StaticResource WidthAdjuster}"&gt; &lt;Binding ElementName="RawControl" Path="ActualHeight" /&gt; &lt;Binding RelativeSource="{RelativeSource AncestorType=Grid}" Path="ActualWidth" /&gt; &lt;Binding RelativeSource="{RelativeSource AncestorType=Grid}" Path="ActualHeight" /&gt; &lt;/MultiBinding&gt; &lt;/local:UserControl1.Width&gt; &lt;/local:UserControl1&gt; &lt;/Viewbox&gt; &lt;/Grid&gt; </code></pre> <p><strong>Here's the MultiValueConverter</strong></p> <pre><code>public class WidthAdjuster : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var rawHeight = (double)values[0]; var containerWidth = (double)values[1]; var containerHeight = (double)values[2]; var ratio = containerWidth / containerHeight; return rawHeight * ratio; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p><strong>Result for 525 x 350 container</strong></p> <p><img src="https://i.stack.imgur.com/IXGAC.png" alt="alt text"></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. This table or related slice is empty.
    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