仅使用 XAML 单击 Windows Phone 时更改按钮背景图像?

Change button background image when click in Windows Phone using XAML only?

所以基本上我想要一个带有特定背景图像的按钮。

例如,加载应用程序时,您会看到一个按钮,其背景图像为 image1.png,然后单击该按钮时,您会看到 image2.png 作为背景图像。然后当您再次单击时,背景图像将切换回 image1.png。

虽然我在C#里做过,但我想在XAML里做,因为每次你点击一个按钮,它会根据主题颜色自动亮起,唯一的办法就是去掉那是通过 XAML.

这是我的代码:

<Button x:Name="Buttons" Content="" HorizontalAlignment="Left" Margin="155,0,0,69" BorderBrush="Transparent" Width="140"  Click="Button_Click" Height="141" VerticalAlignment="Bottom">
                <Button.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Assets/image1.png"/>
                </Button.Background>
            </Button>

提前致谢!

试试这个,

http://visualstudiomagazine.com/articles/2013/02/15/customize-windows-phone-togglebutton.aspx

这里,SDK 附带的 ToggleButton 已被模板化以添加点击和未点击的图像。

带复选框的替代解决方案:

Creating own toggle button in WP8?

VisualStudio 2017 "Blank App"

XAML

        <Button x:Name="button" Content="Button1" HorizontalAlignment="Left" Margin="400,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.258,-5" Click="Button_Click" Height="80" Width="80"/>

C#(在按钮的属性中设置原图:右键->画笔->图像)

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        button1.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("ms-appx:/Images/timerg.png", UriKind.RelativeOrAbsolute)) };

    }

或 C#

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
            BitmapImage bmp = new BitmapImage();
            Uri u = new Uri("ms-appx:/Images/timer.png", UriKind.RelativeOrAbsolute);
            bmp.UriSource = u;
            // NOTE: change starts here
            Image i = new Image();
            i.Source = bmp;
            button1.Content = i;
    }