2013-03-26 61 views
5

在WPF裏面:添加圖像的按鈕編程

<Button Width="24" Height="24" > 
    <Image Source="pack://application:,,,/res/x.png" VerticalAlignment="Center"/> 
</Button> 

我怎麼可以模仿這在C#?我找不到添加子項的Button類中的任何方法。

+1

http://stackoverflow.com/questions/4271277/programmatically-creating-image-button -in-WPF – 2013-03-26 00:20:36

回答

23

ButtonContent控制,所以你只需要使用ButtonsContent財產

例子:

Button myButton = new Button 
{ 
    Width = 24, 
    Height = 24, 
    Content = new Image 
    { 
     Source = new BitmapImage(new Uri("image source")), 
     VerticalAlignment = VerticalAlignment.Center 
    } 
}; 
-3

爲什麼不在XAML中添加圖像並將Source綁定到視圖模型中的屬性?