2009-11-18 39 views
2

如何創建包含填充Colors.Aqua所代表的顏色的Rectangle對象的Button控件?如何創建一個按鈕,使用C#在WPF中填充顏色的矩形對象?

我有一個矩形

Rectangle rectangle = new Rectangle(); 
rectangle.Fill = new SolidColorBrush(Colors.Aqua); 
rectangle.Width = 100; 
rectangle.Height = 50; 

和我有一個按鈕:

Button button = new Button(); 
button.Content = "Button"; 

我無法弄清楚如何將這些東西結合起來。

任何想法?

回答

3
button.Content = rectangle; 

或XAML,這將是像

<Button> 
    <Button.Content> 
    <Rectangle Width="100" Height="50"> 
     <Rectangle.Fill> 
      <SolidColorBrush Color="Aqua" /> 
     </Rectangle.Fill> 
     </Rectangle> 
    </Button.Content> 
</Button> 
+0

謝謝。有時候你會錯過簡單的事情。 ;) – sproketboy 2009-11-18 16:00:07

+0

這不包括「按鈕」文本... – 2009-11-18 16:00:30

+0

如果您需要一個矩形與文本,您需要使用分組控制,如堆疊面板或網格將文本放在矩形(http:///social.msdn.microsoft.com/forums/en-US/wpf/thread/47ee2414-e6d4-4c65-b8cc-25d0f0532ace/) – 2009-11-18 16:22:27

1

你也可以在你的XAML定義樣式,TargetType的設置按鈕,然後設置背景屬性值設置爲「水族」。當然,您還必須爲所有按鈕添加樣式,然後將其放入界面,以便獲得所需的外觀。

相關問題