WPF動態更改Image控件圖片路徑


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之類的,自行百度好啦。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM