WPF动态更改Image控件图片路径

时间:2021-09-07 15:27:05   收藏:0   阅读:19

step 1:先搞个转换器

 1 class ImageConvert : IValueConverter
 2     {
 3         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 4         {
 5             BitmapImage bi = new BitmapImage();
 6             // BitmapImage.UriSource must be in a BeginInit/EndInit block.
 7             bi.BeginInit();
 8             bi.UriSource = new Uri(value.ToString(), UriKind.Absolute);
 9             bi.EndInit();
10             return bi;
11         }
12 
13         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
14         {
15             return null;
16         }
17     }

step 2:在前台页面添加转换器到资源

    <Window.Resources>
        <local:ImageConvert x:Key="ImageConvert"/>
    </Window.Resources>

step 3:在Image中绑定源,及转换器

 <Image x:Name="img" Source="{Binding ViewConfig.HomeBgPath, Converter={StaticResource ImageConvert}}"/>

其它的点,比如Binding之类的,自行百度好啦。

原文:https://www.cnblogs.com/usen521/p/15237036.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!