Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The next code always centers the Canvas element i create.
But my goal is to set my own location, for example, 0,0.
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Canvas canvas = new Canvas();
        canvas.Height = 100;
        canvas.Width = 100;
        canvas.Background = Brushes.Black;

        Canvas.SetTop(canvas, 0);
        Canvas.SetLeft(canvas, 0);

        this.Content = canvas;
    }
}


Notes:
If you write a code of doing it, please do it in C#, not XAML.

The next code sets location to Left, Top.
But my goal is to define exact location.
How to define exact location like axb?
C#
public MainWindow()
{
    InitializeComponent();

    Canvas canvas = new Canvas();
    canvas.Height = 100;
    canvas.Width = 100;
    canvas.Background = Brushes.Black;

    canvas.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
    canvas.VerticalAlignment = System.Windows.VerticalAlignment.Top;

    Canvas.SetTop(canvas, 20);  // doesn't work(
    Canvas.SetLeft(canvas, 20); // doesn't work(
    this.Content = canvas;
}
Posted
Updated 18-Apr-15 0:20am
v6

Normally I'd use Canvas.SetTop, and Canvas.SetLEft and fill in the appropriate values:
Canvas.SetLeft(UIElement,Pos)

But in a element you should set the VerticalAlignment and HorizontalAlignment to place it at the appropriate place. But You should look into Grid and Stackpanels and place the canvas there instead.
 
Share this answer
 
v3
Comments
Ziya1995 18-Apr-15 5:28am    
> Normally I'd use Canvas.SetTop, and Canvas.SetLEft
It doesn't work, i updated the question.

> But where do you place the Canvas then?
I don't modificate WPF app, this is MainWindow.

> If its on a stackpanel it would always center it
Now can you answer? I see 3 options:
1. It is impossible to set location on MainWindow (this).
2. It is possible, you can do it in the next way...

if it is possible, please write a code itself to finish the thread.
At the moment the question is unanswered.
Kenneth Haugland 18-Apr-15 5:33am    
<window x:class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="MainWindow" height="350" width="525">
<grid>
<Canvas HorizontalAlignment="Left" VerticalAlignment="Top" Background="Red" Height="100" Width="100"/>
</grid>
</window>
Ziya1995 18-Apr-15 6:21am    
It worked, but not enough.

It sets location to Left, Top.
But my goal is to define exact location.
How to define exact location like axb?

I updated the question.
Kenneth Haugland 18-Apr-15 6:38am    
Yes, but if you place it in a grid, you can use column and row definitions and place it where you want.

And BTW: Knowing how to use XAML is a must in WPF xD

You can set MArgin property to place it where you want
Ziya1995 18-Apr-15 6:46am    
Solution is Margin.
Solved.
Solution is Margin[^]:
C#
public MainWindow()
{
    InitializeComponent();

    Canvas canvas = new Canvas();

    canvas.Height = 100;
    canvas.Width = 100;

    canvas.Background = Brushes.Black;

    canvas.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
    canvas.VerticalAlignment = System.Windows.VerticalAlignment.Top;
    canvas.Margin = new Thickness(10, 10, 50, 50);

    this.Content = canvas;
}
 
Share this answer
 
v7
Comments
Member 12221842 2-Feb-16 4:14am    
Incase we are using margin but we are not putting to give the hardcode values in the thickness . How?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900