MrEdmundo
MrEdmundo

Reputation: 5165

WPF set image for a button in the C# code behind at runtime

I'm very new to WPF, so please bear with me.

Basically I have defined a Style in a WPF UserControl to show buttons with an image as follows:

<UserControl.Resources>
    <Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <TextBlock  HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Image Width="16" Height="16" Stretch="UniformToFill"/>
                        <ContentPresenter/>
                    </TextBlock>
                </ControlTemplate>
            </Setter.Value>

        </Setter>
    </Style>
</UserControl.Resources>

I then add a load of buttons to a grid at runtime (it has to be at run time as the number and type of button is dynamic).

What I would like to do is set the image of the buttons at runtime as well. I have tried a number of ways, but none seem to work. Setting the source in the Xaml works fine. The code I'm trying is as follows:

Button b = new Button();
// Create source.
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\SourceCode\DSA\DSALibrary\Icons\money.png");
bi.EndInit();

b.SetValue(Image.SourceProperty, bi);

Could anybody point me towards where I'm going wrong, if I were to guess, I would say that where I think I'm setting the value, I'm actually not.

Cheers

Upvotes: 1

Views: 10881

Answers (2)

Arcturus
Arcturus

Reputation: 27055

You can try to use the Content property of Button, and declare a DataTemplate for handling the content:

<UserControl.Resources>
    <Style TargetType="Button">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Stretch="UniformToFill" Source="{Binding}"/>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="My button text" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</UserControl.Resources>

Set the BitmapImage on the content of your button, et voila :)

Upvotes: 3

Jon Cage
Jon Cage

Reputation: 37448

Can you confirm that it's a System.Windows.Controls.UserControl derived object you're creating?

My guess would be that the control loads it's bitmap when you create it so setting the image source property after that isn't being caught. Have you tried creating a button programmatically with a two-stage creation and setting it before you do the second stage?

You could try calling the button's Invalidate() function to make sure it redraws.

Upvotes: 0

Related Questions