我想有一個按鈕,同時包含一個圖像文本。理想情況下文本左對齊,而圖像儘可能正確。Silverlight 3的按鈕 - 文字+圖片對齊問題
所以我創建了一個網格,並添加圖像和文本塊給它,並設置其對齊。對於我的生活,我不能按照預期行事。
var gridPanel = new Grid();
gridPanel.ColumnDefinitions.Add(new ColumnDefinition());
var text = new TextBlock { Text = header, TextAlignment = TextAlignment.Left };
text.HorizontalAlignment = HorizontalAlignment.Left;
text.Margin = new Thickness(0);
text.SetValue(Grid.RowProperty, 0);
text.SetValue(Grid.ColumnProperty, 0);
var image = new Image();
image.Source = new BitmapImage(new Uri("../Images/Common/RedFlag.png", UriKind.Relative));
image.HorizontalAlignment = HorizontalAlignment.Right;
image.Height = 25;
image.Width = 25;
image.SetValue(Grid.RowProperty, 0);
image.SetValue(Grid.ColumnProperty, 1);
gridPanel.Children.Add(text);
gridPanel.Children.Add(image);
button.Content = gridPanel;
由於某種原因,圖像和文本都是居中對齊的......網格是錯誤的路要走嗎?我試過的StackPanel和方向設置爲水平,但它是同樣的事情..
+1:永遠不知道你能做到這一點 – 2011-01-20 16:00:11