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

如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!

浙公网安备 33010602011771号