简体   繁体   中英

Adding an Image inside a Button programmatically

In WPF:

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

How can I mimic this in C#? I can't find any method in the Button class that adds children.

Button is a Content control so you just have to use the Buttons Content property

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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