以编程方式在按钮内添加图像

本文关键字:添加 图像 按钮 编程 方式 | 更新日期: 2023-09-27 18:05:43

在WPF:中

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

我如何在C#中模仿这一点?在Button类中找不到任何添加子级的方法。

以编程方式在按钮内添加图像

ButtonContent控件,因此您只需使用Buttons Content属性

示例:

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