如何更改按钮的背景图像上运行? [英] How to change the background image of button on runtime?

查看:206
本文介绍了如何更改按钮的背景图像上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米坚持一个问题。我想改变按钮的背景图像上运行。我得到了改变颜色的解决方案,但我想改变形象。

在code如下:

 公共无效buttonCase(对象发件人,RoutedEventArgs E)
{
    开放的我们的uri = NULL;
    VAR形象=新的ImageBrush();
    如果(((APP)App.Current).appControler.m_Mode == Controller.textMode.Letters)
    {
        ((APP)App.Current).appControler.buttonCase(发件人,E);
        开关(((APP)App.Current).appControler.m_case)
        {
        案例Controller.caseMode.Upper:
            b0.FontSize = b1.FontSize = b2.FontSize = b3.FontSize = b4.FontSize = b5.FontSize = b6.FontSize = b7.FontSize
            = b8.FontSize = b9.FontSize = bCornerLower.FontSize = 30.0;
            URI =新的URI(@/来源$ C ​​$ C,分量/图片/ Lower_Case_p.png,UriKind.Relative);
            image.ImageSource =新的BitmapImage(URI);
            btnCase.Background =图像;
            打破;
        案例Controller.caseMode.Lower:
            b0.FontSize = b1.FontSize = b2.FontSize = b3.FontSize = b4.FontSize = b5.FontSize = b6.FontSize = b7.FontSize
            = b8.FontSize = b9.FontSize = bCornerLower.FontSize = 40.0;
            URI =新的URI(@/来源$ C ​​$ C,分量/图片/ Case_p.png,UriKind.Relative);
            image.ImageSource =新的BitmapImage(URI);
            btnCase.Background =图像;
            打破;
        }
    }
}
 

解决方案

这样的事情?

  VAR刷=新的ImageBrush();
brush.ImageSource =新的BitmapImage(新的URI(@图像/ myImage.png,UriKind.Relative));
myButton.Background =刷;
 

我做了一个测试应用程序与两个图像。我切换按钮点击图像和它的作品。

 私人布尔标志;

私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
    标志=标志!;

    VAR uriString中=标志? @图像/ logo.png:@图像/的icon.png;
    myButton.Background =新的ImageBrush
        {=的ImageSource新的BitmapImage(新的URI(uriString中,UriKind.Relative))};
}
 

I m stuck with a problem. I want to change the background image of button on runtime. I got the solution for changing the color but i want to change the image.

The code is as follows

public void buttonCase(object sender, RoutedEventArgs e)
{
    Uri uri = null;
    var image = new ImageBrush();
    if (((App)App.Current).appControler.m_Mode == Controller.textMode.Letters)
    {
        ((App)App.Current).appControler.buttonCase(sender, e);
        switch (((App)App.Current).appControler.m_case)
        {
        case Controller.caseMode.Upper:
            b0.FontSize = b1.FontSize = b2.FontSize = b3.FontSize = b4.FontSize = b5.FontSize = b6.FontSize = b7.FontSize
            = b8.FontSize = b9.FontSize = bCornerLower.FontSize = 30.0;
            uri = new Uri(@"/SourceCode;component/Images/Lower_Case_p.png", UriKind.Relative);
            image.ImageSource = new BitmapImage(uri);
            btnCase.Background = image;
            break;
        case Controller.caseMode.Lower:
            b0.FontSize = b1.FontSize = b2.FontSize = b3.FontSize = b4.FontSize = b5.FontSize = b6.FontSize = b7.FontSize
            = b8.FontSize = b9.FontSize = bCornerLower.FontSize = 40.0;
            uri = new Uri(@"/SourceCode;component/Images/Case_p.png", UriKind.Relative);
            image.ImageSource = new BitmapImage(uri);
            btnCase.Background = image;
            break;
        }
    }
}  

解决方案

Something like this?

var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
myButton.Background = brush;

[Edit] I made a test application with two images. I toggle the image on button click and it works.

private bool flag;

private void button1_Click(object sender, RoutedEventArgs e)
{
    flag = !flag;

    var uriString = flag ? @"Images/logo.png" : @"Images/icon.png";
    myButton.Background = new ImageBrush 
        { ImageSource = new BitmapImage(new Uri(uriString, UriKind.Relative)) };
}

这篇关于如何更改按钮的背景图像上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆