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

标签 c# wpf button children

在 WPF 中:

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

我如何在 C# 中模仿它?我在 Button 类中找不到任何添加子项的方法。

最佳答案

Button 是一个 Content 控件,因此您只需使用 Buttons Content 属性

例子:

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

关于c# - 以编程方式在按钮内添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627123/

相关文章:

c# - 为什么它总是抛出 ObjectDisposedException?

c# - SQL SERVER 删除大量行优化

WPF 通知

iphone - iPhone SDK 中的透明按钮

c# - Azure EventHub PartitionedSender 的重试逻辑

c# - 是否可以在 C# 中定义参数化类型别名?

c# - 使用 ProcessID 或窗口句柄获取子窗口?

c# - 在代码隐藏文件中设置报表查看器的 rdlc 文件的相对路径

html - 如何用固定宽度换行 HTML 按钮的文本?

android - 焦点在 GridView 布局中