WPF - 代码动态更换按钮的背景图片

WPF - 代码动态更换按钮的背景图片
文章目录
1 相对路径
2 绝对路径方式
2.1 如果图片没有导入到项目中
2.2 如果图片已经导入到项目中
1 相对路径
将在项目exe路径Resourc目录下寻找图片

ImageBrush brush1 = new ImageBrush();
string path = @"Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Relative));
brush1.ImageSource = img;
Testbutton.Background = brush1;

2 绝对路径方式
2.1 如果图片没有导入到项目中
ImageBrush brush1 = new ImageBrush();
string path = @"F:/Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
brush1.ImageSource = img;
Testbutton.Background = brush1;

2.2 如果图片已经导入到项目中
使用@"pack://application:标志为当前项目

ImageBrush brush1 = new ImageBrush();
string path = @"pack://application:,,,/Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
brush1.ImageSource = img;
Testbutton.Background = brush1;
 

猜你喜欢

转载自blog.csdn.net/u014090257/article/details/130961919
今日推荐