I'm trying to center a textblock in my app window. I tried this code inside the textblock's Loaded event handler:
private void textBlock1_Loaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("textBlock1_Loaded");
double textBlockWidth = textBlock1.Width;
double textBlockHeight = textBlock1.Height;
double gridWidth = grid1.Width;
double gridHeight = grid1.Height;
double leftRightMargin_center = (gridWidth - textBlockWidth) / 2;
double topBottomMargin_center = (gridHeight - textBlockHeight) / 2;
double topMargin_needed = topBottomMargin_center - 25;
double bottomMargin_needed = topBottomMargin_center + 25;
double leftMargin_needed = leftRightMargin_center;
double rightMargin_needed = leftRightMargin_center;
textBlock1.Margin = new Thickness(leftMargin_needed, topMargin_needed, rightMargin_needed, leftMargin_needed);
}
Here's the resulting app:
Am I doing something wrong in here? And yes, that's the only question I have in this thread.
