I am creating a project where I am creating an entire project (including a template) using C # in Visual Studio.
I created a template and just started working on events, I want to create a MouseLeftButtonDown event for the created canvas, this event will register the mouse position.
This is my code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private char tester;
private Point down1;
private Point up1;
private void Grid1_Loaded(object sender, RoutedEventArgs e)
{
RowDefinition row1 = new RowDefinition();
RowDefinition row2 = new RowDefinition();
row1.Height = new GridLength(40);
row2.Height = new GridLength(1, GridUnitType.Star);
Grid1.RowDefinitions.Add(row1);
Grid1.RowDefinitions.Add(row2);
Canvas canvas1 = new Canvas();
canvas1.Background = new SolidColorBrush(Colors.Beige);
Grid.SetRow(canvas1, 1);
Grid1.Children.Add(canvas1);
canvas1.MouseLeftButtonDown += new RoutedEventHandler(canvas1_MouseLeftButtonDown);
canvas1.MouseLeftButtonUp += new RoutedEventHandler(canvas1_MouseLeftButtonUp);
Button btnRect = new Button();
btnRect.Content = "Rectangulo";
btnRect.Margin = new Thickness(5, 5, 5, 5);
btnRect.HorizontalAlignment = HorizontalAlignment.Left;
btnRect.VerticalAlignment = VerticalAlignment.Center;
Grid.SetRow(btnRect, 0);
Grid1.Children.Add(btnRect);
Button btnEllip = new Button();
btnEllip.Content = "Ellipse";
btnEllip.Margin = new Thickness(75, 5, 5, 5);
btnEllip.HorizontalAlignment = HorizontalAlignment.Left;
btnEllip.VerticalAlignment = VerticalAlignment.Center;
Grid.SetRow(btnEllip, 0);
Grid1.Children.Add(btnEllip);
}
And this is my event:
private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
down1 = e.GetPosition(canvas1);
}
}
}
I tried to do it down1 = e.GetPosition(canvas1);, but I get an error (canvas1); "The name canvas1 does not exist in the current context"
I did a similar project without having to code the template without any problems ..
source
share