I am trying to move and reposition elements with mouse and through code. But I guess I'm missing something or do something the wrong way. So I built a little sample app. It's just a empty wpf app with this MainWindow function
public MainWindow()
{
InitializeComponent();
Label lText = new Label();
lText.Content = "this is my test label";
lText.Height = 50;
lText.Width = 50;
lText.Background = Brushes.Aqua;
// do I really need to do this?
lText.VerticalAlignment = System.Windows.VerticalAlignment.Top;
lText.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
// this part already fails
Canvas.SetTop(lText, 20);
Canvas.SetLeft(lText, 10);
this.Content = lText;
}
The solution was that the Left and Top properties must be set inside the Xaml before you can read it. I was always getting NaN and therefor couldn't set the correct value either.