views:

544

answers:

2

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).

A XAML only solution (using property triggers or similar) is prefered.

Your help is much appreciated as always.

+1  A: 

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).

TomiJ
Briljant! Briljant (I need 15 characters at least to tell you so)
Bart Roozendaal
A: 
<Ellipse x:Name="anEllipse" Width={Binding Path=ActualHeight ElementName=anEllipse} />

You could probably get away with not naming this if you did a relative binding as well.

Paul Betts
What if the height of the bounding box is greater than the width? In that case I want the Height=ActualWidth.
Bart Roozendaal
In this case, you need to write code to do this - i.e. you want to do greedy sizing that maintains the aspect ratio (kind of like how a movie player does it, that switches which side it bases its sizes on based on which axis is bigger)
Paul Betts
That's what I thought. Too bad, but no problem. Thanks
Bart Roozendaal