【wpf】Image 图片绑定/更新

前台代码:

<Image   Source="{Binding PreviewImage}"></Image>

后台代码:(这里采用了prsim的写法,可以参考我的psrim系列文章,也可以采用其他形式的通知写法)

//绑定对象
private ImageSource _previewImage;
public ImageSource PreviewImage
{
    get { return _previewImage; }
    set { SetProperty(ref _previewImage, value); }
}


//----------更新图片显示
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri($"{AppDomain.CurrentDomain.BaseDirectory}/{path}", UriKind.Absolute);
bi.EndInit();
PreviewImage = bi; //完成图片的更新

小结:

        从目前测试结果看,图片的路径只能通过完整的路径,如果通过相对路径的写法,必须提前将图片设置为资源。

posted @   宋桓公  阅读(217)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2020-10-18 xadmin卡顿延迟的原因
2017-10-18 Android 真机调试
点击右上角即可分享
微信分享提示