Wilson
Wilson

Reputation: 8778

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.

Upvotes: 8

Views: 22593

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43636

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
    }
};

Upvotes: 29

Related Questions