【问题标题】:About Button Images in WP7关于 WP7 中的按钮图像
【发布时间】:2011-10-19 10:30:04
【问题描述】:

我正在尝试为 WP7 开发应用程序,如果单击按钮,我想更改按钮的背景图像。我该怎么做?

【问题讨论】:

    标签: c# windows xaml mobile


    【解决方案1】:

    您需要像这样为button 创建一个DataTemplate

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

    然后您可以在 XAML 中像这样在按钮上设置图像

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

    在按钮点击事件后面的代码中,您可以这样做以在运行时更改背景

    button.Content = YourImage;
    

    【讨论】:

    • 对不起,我是 xaml 的新手。我要在哪里粘贴 xaml 代码?
    • 您可以将此代码粘贴到页面的resources 部分,或控制您的方案中的任何内容。
    • 我的意思是带有
    • 你把它放在一个资源容器中。请参阅本教程weblogs.asp.net/scottgu/pages/… 或搜索如何在 XAML 中使用样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多