hellzone
hellzone

Reputation: 5236

How to resize multiple canvas in WPF?

I want to resize multiple shapes at same time.Here is my xaml code. It has 2 canvas called g6 and g10 but in real project i got nearly 100 canvas. Any solution?

<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="svg2"
    Width="612"
    Height="800">
    <Canvas
        x:Name="g4"
        RenderTransform="1.3702001,0,0,1.4522231,268.12186,265.31238">
        <Canvas x:Name="g6">
            <Path
                x:Name="path8"
                Data="F1 M-153.219,-37.369C-153.219,-37.369 -153.168,-36.483 -153.579,-36.491 -153.99,-36.5 -162.189,-58.998 -172.418,-57.948 -172.419,-57.948 -163.557,-61.389 -153.219,-37.369L-153.219,-37.369z"
                Fill="#FFFFFFFF"
                Stroke="#FF000000"
                StrokeMiterLimit="4"
                StrokeThickness="0.172" />
        </Canvas>
        <Canvas x:Name="g10">
            <Path
                x:Name="path12"
                Data="F1 M-151.46,-38.783C-151.46,-38.783 -151.734,-37.933 -152.117,-38.073 -152.5,-38.212 -152.06,-61.988 -162.059,-64.269 -162.059,-64.269 -152.48,-64.673 -151.46,-38.783z"
                Fill="#FFFFFFFF"
                Stroke="#FF000000"
                StrokeMiterLimit="4"
                StrokeThickness="0.172" />
        </Canvas>
    </Canvas>
</Canvas>

Upvotes: 1

Views: 1168

Answers (3)

akjoshi
akjoshi

Reputation: 15772

Based on the current information -

You can create Width and Height properties in your ViewModel(or CodeBehind or whatever object you are setting as DataContext of your Window/usercontrol) and bind Width and Height of your each Canvas to them; Changing these properties in code will resize all the canvas in your xaml.

In case you don't want to add bindings to each canvas you can create a global style for Canvas(without x:Key), which will automatically be applied to each Canvas; but you will have to be careful in putting this style in correct place, in the current xaml you have provided adding this style to your top most Canvas's resources will make sense.

something like this -

<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        x:Name="svg2" Width="612" Height="800" >
    <Canvas.Resources>
        <Style TargetType="{x:Type Canvas}">
            <Setter Property="Width" Value="{Binding GlobalWidth}" />
            <Setter Property="Height" Value="{Binding GlobalHeight}" />
        </Style>
    </Canvas.Resources>

    <Canvas x:Name="g4" ... />
    <Canvas x:Name="g6" ... />
    <Canvas x:Name="g10" ... />
</Canvas>

As a sidenote, I would suggest you to not rely on converted XAML as it is; try and refactor it to use appropriate conrols/panels, extract relevant parts into usercontrols, use styles/triggers etc.

Upvotes: 1

zeencat
zeencat

Reputation: 590

In your code you can do something like this:

IEnumerable<Canvas> Collection = RootControl.Chidren.ofType<Canvas>();
foreach(Canvas c in Collection)
{
    c.Height = NewHeight;
    c.Width = NewWidth;
}

Upvotes: 1

mlemay
mlemay

Reputation: 1637

You can create a Viewbox that will contain all your canvas... Give us more information about your project.

Upvotes: 0

Related Questions