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

Wil*_*son 7 c# wpf children button

在WPF中:

<Button Width="24" Height="24" >
    <Image Source="pack://application:,,,/res/x.png" VerticalAlignment="Center"/>
</Button>
Run Code Online (Sandbox Code Playgroud)

我怎样才能在C#中模仿这个?我在Button类中找不到添加子项的任何方法.

sa_*_*213 25

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
    }
};
Run Code Online (Sandbox Code Playgroud)

  • 如果我想在带有文本内容的按钮上添加图像怎么办? (2认同)