Wpf静态动态资源绑定 图片资源
Wpf静态动态资源绑定 图片资源
1:静态资源显示很简单
<Image Source ="一个全路径即可.png" Stretch="Fill"></Image>
2:动态资源显示稍微比较繁琐些
<Window x:Class="CloudHotelClient.View._00.Base.HM_Agreement_ComstomerShowImg" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:CloudHotelClient.View._00.Base" xmlns:cc="clr-namespace:CloudHotelClient.Common" mc:Ignorable="d" Title="查看签名图片" Height="500" Width="850" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow"> <Window.Resources> <cc:StringToImageSourceConverter x:Key="ccimgkey" ></cc:StringToImageSourceConverter> </Window.Resources> <Grid> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <Image Source ="{Binding Path=GetImgPath, Converter={StaticResource ccimgkey}}" Stretch="Fill"></Image> </StackPanel> </Grid> </Window>
定义转换器
/// <summary> /// 字符串同图像资源 jason /// </summary> public class StringToImageSourceConverter : IValueConverter { #region Converter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string path = (string)value; if (!string.IsNullOrEmpty(path)) { return new BitmapImage(new Uri(path, UriKind.Absolute)); } else { return null; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } #endregion }
View后端
/// <summary> /// HM_Agreement_ComstomerShowImg.xaml 的交互逻辑 /// </summary> public partial class HM_Agreement_ComstomerShowImg : Window { public HM_Agreement_ComstomerShowImg() { InitializeComponent(); this.DataContext = new HM_Agreement_ComstomerShowImgViewModel(); } }
ViewModel
using GalaSoft.MvvmLight; using System.ComponentModel; namespace CloudHotelClient.ViewModel { public class HM_Agreement_ComstomerShowImgViewModel:ViewModelBase { public event PropertyChangedEventHandler PropertyChangedThis; public void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChangedThis?.Invoke(this, e); } public static string fullImgPath; private string _getimgPath; public string GetImgPath { get { return _getimgPath; } set { _getimgPath = value; RaisePropertyChanged(() => GetImgPath); } } public HM_Agreement_ComstomerShowImgViewModel() { GetImgPath=fullImgPath; } } }
如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!
分类:
WPF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具