kmetin
kmetin

Reputation: 263

About Button Images in WP7

I am trying to develop an app for WP7 and I want to change the background image of a button if it is clicked. How can I do that?

Upvotes: 0

Views: 242

Answers (1)

Haris Hasan
Haris Hasan

Reputation: 30097

You need to create a DataTemplate for button like this

<Style TargetType="Button">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="16" Height="16" Stretch="UniformToFill" Source="{Binding}"/>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Then you can set the image on button like this in XAML

<Button x:Name="button"  Content="{StaticResource ResourceKey=MyImageSource}"></Button>

In code behind inside the button click event you can do this to change the background at runtime

button.Content = YourImage;

Upvotes: 2

Related Questions