MNie
MNie

Reputation: 1367

Adding children to the Canvas

I have a problem with adding childrens (lines) to the canvas, when I debug my code I see that canvas has childrens which I add, but I didn't see they on the canvas: Here is my code:

xaml:

<Grid x:Name="CanvasGrid" Grid.Column="0" Grid.Row="1">
    <Canvas Background="Bisque" x:Name="PatyczakCanvas"></Canvas>
</Grid>

c#

var _body = new Line
{
    X1 = 120,
    X2 = 120,
    Y1 = 20,
    Y2 = 75,
    Width = 20.0,
    Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)),
    StrokeThickness = 20,
    Visibility = Visibility.Visible
};
_body.MouseLeftButtonDown += _BodyLeftMouseButtonEventHandler;
PatyczakCanvas.Children.Add(_body);

and example of manipulating the canvas: Slider value changes method:

_body.X2 = _body.X1 + DlugoscSlider.Value * Math.Sin(KatSlider.Value * Math.PI / 180.0);
_body.Y2 = _body.Y1 + DlugoscSlider.Value * Math.Cos(KatSlider.Value * Math.PI / 180.0);

I also try to add lines to the grid instead of canvas, but when I manipulate the line, their width/height in which they are draw didn't changed..

If someone could help me I will be very grateful!

Upvotes: 1

Views: 373

Answers (1)

Dmitry Popov
Dmitry Popov

Reputation: 398

You should change or remove the Width property:

var _body = new Line
{
    X1 = 120,
    X2 = 120,
    Y1 = 20,
    Y2 = 75,
    Stroke = Brushes.Red,
    StrokeThickness = 20,
};

The Width affects the visible area of the line.

You can see the effect if you use a higher value, e.g. 150:

enter image description here

In your code, the Width is less than X1 and X2, and the line is drawn beyond the visible area.

Upvotes: 1

Related Questions