Liam
Liam

Reputation: 513

WPF Canvas Fill

I currently have a WPF windows with a Canvas is 600 x 400. Is it possible to scale or automatically zoom in so that the lines take up as much as the 600x600 as possible?

   <Border>
          <Canvas x:Name="cMap" Width="600" Height="400">
                 <Line X1="5" Y1="5" X2 ="10" Y2="10" StrokeThickness="2" Stroke="Black"/>
                 <Line X1="10" Y1="10" X2 ="15" Y2="25" StrokeThickness="2" Stroke="Black"/>
          </Canvas>
   </Border>

My intention will be to add lines programmatically via code instead of XAML.

Thanks.

Upvotes: 0

Views: 3020

Answers (2)

user8207463
user8207463

Reputation:

Hope this helps you!

To draw lines in code you shoud do something like this:

Line line = new Line();
    Thickness thickness = new Thickness(101,-11,362,250);
    line.Margin = thickness;
    line.Visibility = System.Windows.Visibility.Visible;
    line.StrokeThickness = 4;
    line.Stroke = System.Windows.Media.Brushes.Black;
    line.X1 = 10;
    line.X2 = 40;
    line.Y1 = 70;
    line.Y2 = 70;

and don't forget to add:

myCanvas.Children.Add(line); 

to put those line in some place

from: Drawing lines in code using C# and WPF

To resize your canvas please read this:

Canvas is the only panel element that has no inherent layout characteristics. A Canvas has default Height and Width properties of zero, unless it is the child of an element that automatically sizes its child elements. Child elements of a Canvas are never resized, they are just positioned at their designated coordinates. This provides flexibility for situations in which inherent sizing constraints or alignment are not needed or wanted. For cases in which you want child content to be automatically resized and aligned, it is usually best to use a Grid element.

So as a solution you would make it inside a GRID or using the following code:

public class CanvasAutoSize : Canvas
{
protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
{
    base.MeasureOverride(constraint);
    double width = base
        .InternalChildren
        .OfType<UIElement>()
        .Max(i => i.DesiredSize.Width + (double)i.GetValue(Canvas.LeftProperty));

    double height = base
        .InternalChildren
        .OfType<UIElement>()
        .Max(i => i.DesiredSize.Height + (double)i.GetValue(Canvas.TopProperty));

    return new Size(width, height);
}
}

at your XAML:

 <local:CanvasAutoSize VerticalAlignment="Top" HorizontalAlignment="Left"></local:CanvasAutoSize>

from: WPF: How to make canvas auto-resize?

Upvotes: 2

Dipen Shah
Dipen Shah

Reputation: 26085

Not sure what is your exact usecase, but you could probably benefit by using ViewBox:

<Border>
    <Viewbox Stretch="Uniform">
        <Canvas x:Name="cMap" Width="15" Height="25">
            <Canvas.LayoutTransform>
                <ScaleTransform />
            </Canvas.LayoutTransform>
            <Line X1="5" Y1="5" X2 ="10" Y2="10" StrokeThickness="2" Stroke="Black"/>
            <Line X1="10" Y1="10" X2 ="15" Y2="25" StrokeThickness="2" Stroke="Black"/>
        </Canvas>
    </Viewbox>
</Border>

Upvotes: 2

Related Questions