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;
        }
    }
}
复制代码

 

posted @   天天向上518  阅读(407)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示