0

I have a button with a template.

I would like to dynamically change the image source from the code behind.

Here is my code:

<Button Height="48" HorizontalAlignment="Left" Name="playSequence" VerticalAlignment="Top" Width="49" Click="PlaySequenceButton_Click" Grid.Column="1" Margin="10,0,0,0">
    <Button.Template>
        <ControlTemplate>
            <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                <Image Name="PlayImage" Source="Play.png" Width="45" Height="41" Stretch="Fill"/>
            </Border>
        </ControlTemplate>
   </Button.Template>
</Button>

When I type this code:

PlayImage.Source = new BitmapImage(new Uri(@"Pause.png", UriKind.Relative));

PlayImage is not recognized.

Is there any way to change the button's image source that way?

2 Answers 2

1

You can try using the FindName method of a FrameworkTemplate:

playSequence.Template.FindName("PlayImage", playSequence)
            .SetValue(Image.SourceProperty, 
                      new BitmapImage(new Uri(@"Pause.png", UriKind.Relative)));
Sign up to request clarification or add additional context in comments.

Comments

0

You might prefer to set the image by means of the Button's Content property in a Style like this:

<Button ...>
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border ...>
                            <Image Source="{TemplateBinding Content}"
                                   Width="45" Height="41" Stretch="Fill"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Content">
                <Setter.Value>
                    <BitmapImage UriSource="Play.png"/>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

Now you could simply change the image by setting the Content property:

playSequence.Content = new BitmapImage(new Uri(@"Pause.png", UriKind.Relative));

4 Comments

Thanks. That helped. By the way, is there any way to give the button back its click effect?
Not sure what you mean by click effect.
Before I added the image to the button, the button had a 3D visual behavior when it was clicked. Now the button is static, I can see only the image, even if I click it. There is no indication that the button was clicked. Is there any way to 'restore' that visual effect when I click the button?
That is because the default Button Style is a lot more complex than what we have here. See Button Styles and Templates to get an overview and pay special attention to the different Visual States.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.