views:

265

answers:

1

Hi,

This should be simple, but I guess I'm missing something.

I have a simple user control containing a grid and 2 paths.

I would like to make this user control stretchable to any desired size, keeping the same relative position of the paths (the original work has much more paths with animation).

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="test.MainPage">
    <Grid x:Name="LayoutRoot" Background="White" Width="400" Height="400">
        <Path Fill="White" Stretch="Fill" Stroke="Black" Height="101"
              Margin="49.5,49.5,199.5,0" VerticalAlignment="Top"
              Data="M50,50 L100,50 L150,50 L200,50 L200,100 L150,100 L150,150 L100,150 L100,100 L50,100 z"/>
        <Path Fill="White" Stretch="Fill" Stroke="Black"
              Margin="0,150.5,48.5,148.5"
              Data="M50,50 L100,50 L150,50 L200,50 L200,100 L150,100 L150,150 L100,150 L100,100 L50,100 z"
              HorizontalAlignment="Right" Width="151"/>
    </Grid>
</UserControl>

Thanks for your help.

+2  A: 

Changing LayoutRoot to a ViewBox will make the paths resize as the control resizes:

<Viewbox x:Name="LayoutRoot" Stretch="Fill">
    <Grid Width="400" Height="400">
        <Path Fill="White" Stretch="Fill" Stroke="Black" Height="101" 
            Margin="49.5,49.5,199.5,0" VerticalAlignment="Top" 
            Data="M50,50 L100,50 L150,50 L200,50 L200,100 L150,100 L150,150 L100,150 L100,100 L50,100 z"/>
        <Path Fill="White" Stretch="Fill" Stroke="Black" 
            Margin="0,150.5,48.5,148.5" 
            Data="M50,50 L100,50 L150,50 L200,50 L200,100 L150,100 L150,150 L100,150 L100,100 L50,100 z" 
            HorizontalAlignment="Right" Width="151"/>
    </Grid>
</Viewbox>

Edit: reply to comments.

I tried it in Silverlight 3 and it worked with the following markup allowing the figures to rescale as the browser window size changes:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
x:Class="SLViewbox.UserControl1"
>

<Grid x:Name="LayoutRoot">
    <controlsToolkit:Viewbox>
        <Grid Width="400" Height="400"> 
        <Path Fill="White" Stretch="Fill" Stroke="Black" Height="101"  
            Margin="49.5,49.5,199.5,0" VerticalAlignment="Top"  
            Data="M50,50 L100,50 L150,50 L200,50 L200,100 L150,100 L150,150 L100,150 L100,100 L50,100 z"/> 
        <Path Fill="White" Stretch="Fill" Stroke="Black"  
            Margin="0,150.5,48.5,148.5"  
            Data="M50,50 L100,50 L150,50 L200,50 L200,100 L150,100 L150,150 L100,150 L100,100 L50,100 z"  
            HorizontalAlignment="Right" Width="151"/> 
        </Grid>
    </controlsToolkit:Viewbox>
</Grid>
</UserControl>

The user control is embedded in a page:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SLViewbox" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="SLViewbox.MainPage"
mc:Ignorable="d">

<Grid x:Name="LayoutRoot" Background="White">
    <local:UserControl1 />
</Grid>
</UserControl>
Doug Ferguson
Ops.. I forgot to say that I need it for silverlight and not WPF. I assumed it was the same but it seems that SL doesn't have Viewbox.I tried the SL toolkit which has Viewbox but I couldn't get it to work (throws exceptions..)Can you think of another way?
Gilad
A viewbox should work fine with this. Can you elaborate on the exceptions that are thrown? (SL4 has a viewbox built in by the way - it's exactly the same code as the toolkit)
Alun Harford