gpt4 book ai didi

c# - 如何在运行时更改按钮的背景图像?

转载 作者:太空狗 更新时间:2023-10-30 00:06:00 25 4
gpt4 key购买 nike

我遇到了一个问题。我想在运行时更改按钮的背景图像。我得到了更改颜色的解决方案,但我想更改图像。

代码如下

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

最佳答案

是这样的吗?

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

[编辑] 我制作了一个包含两张图片的测试应用程序。我在单击按钮时切换图像,效果很好。

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)) };
}

关于c# - 如何在运行时更改按钮的背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6327074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com