简体   繁体   中英

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM