1
我試圖在我的應用程序窗口中居中放置一個文本塊。我想這個代碼文本塊的Loaded事件處理中:UWP C中的居中控件#
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);
}
下面是導致應用程序:
我是不是做錯了這裏?是的,這是我在這個線程中唯一的問題。
什麼樣的容器控件是文本塊中? –
現在,在WPF中,我只是在XAML中使用HorizontalAlignment = Center,但我是一個混蛋。通常,在任何基於XAML的用戶界面中,您不必在代碼隱藏中執行任何這種Windows窗體式的瘋狂行爲。你的XAML是什麼樣的? – Will
@KenTucker它只是在一個網格 –