简体   繁体   English

以编程方式在Button内添加图像

[英]Adding an Image inside a Button programmatically

In WPF: 在WPF中:

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

How can I mimic this in C#? 我怎样才能在C#中模仿这个? I can't find any method in the Button class that adds children. 我在Button类中找不到添加子项的任何方法。

Button is a Content control so you just have to use the Buttons Content property Button是一个Content控件,因此您只需使用Buttons Content属性

Example: 例:

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

为什么不在XAML中添加Image并将Source绑定到视图模型中的属性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM