wpftriggerspropertiesellipse

In XAML, how can I keep an ellipse being a circle?


I'm getting XAML-blind I'm afraid. I'm developing a MS Surface application, and I have an ellipse inside a ScatterViewItem (a container an end user can resize). I would like to keep the ellipse a circle (width == height) and keep it as big as possible (the lowest value of width/height of the SVI should be taken for both width/height properties of the ellipse).

An XAML only solution (using property triggers or similar) is preferred.

Your help is much appreciated as always.


Solution

  • Would a simple Viewbox do the trick? E.g.

    <Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        HorizontalAlignment="Center"
        VerticalAlignment="Center">
        <Canvas Width="100" Height="100">
            <Ellipse Fill="Red" Width="100" Height="100" />
        </Canvas>
    </Viewbox>
    

    The Viewbox will scale its contents to fill the area of the Viewbox, and by default does the scaling proportionally. The specified horizontal and vertical alignments keep the Ellipse centered when it cannot be stretched to the full size (because of the proportional scaling).