0

在 Windows 8 Metro 应用程序中,我使用 xaml 添加了一个按钮,如下所示

<Button Foreground="#FFF51A1A" Background="#FFEAE23A" BorderBrush="White" 
Height="100" Width="100" HorizontalAlignment="Center" Content="HELLO"/>

以及我尝试动态添加的相同按钮,如下所示

        Button shelfButton = new Button();

        shelfButton.Content = "HELLO";
        shelfButton.Foreground = FFF51A1A;
        shelfButton.Background = FFEAE23A;

        shelfButton.Height = 100;
        shelfButton.Width = 100;

        Grid.Children.Add(shelfButton);

按钮在前景色和背景色方面的行为是不同的。为什么会这样?以及如何在动态添加按钮时具有与 xaml 类似的行为。

4

1 回答 1

1

要获得等效的行为,您可以在代码中将shelfButton 的Foreground 和Background 设置为SolidColorBrush。在http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/befe7695-9458-4abb-b867-619df52b8183上有一个 HEX(“#FFF51A1A”)到 SolidColorBrush 转换器。

于 2013-02-19T14:09:21.893 回答