Friday, November 22, 2013

To get or set the position of a textbox or a button in WPF

To get the position of a textbox or a button

Point p2 = mybox.TransformToAncestor(Application.Current.MainWindow).Transform(new Point(0, 0))

Debug.WriteLine("here: " + p2.X + ", " + p2.Y);

To set the position of a canvas:
Canvas c = new Canvas();
c.Margin = new Thickness(X, Y, 0, 0);

To set the position of a button within a canvas:
Button b = new Button();
b.Content = "asdf";
b.Width = 50;
b.Height = 22;
b.Click += new RoutedEventHandler(b_Click);
c.Children.Add(b);

Canvas.SetLeft(b, 20);
Canvas.SetTop(b, 40);

private void b_Click(object sender, RoutedEventArgs e) {
Debug.WriteLine("good");
}

No comments: