100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > WPF - 代码动态更换按钮的背景图片

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

时间:2021-11-27 17:02:34

相关推荐

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;

如果有兴趣,可以访问我的个人站:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。