2013-03-26 56 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绑定到视图模型中的属性?