其他分享
首页 > 其他分享> > 【WPF】Image绑定图片

【WPF】Image绑定图片

作者:互联网

一、静态绑定

<Image Source="/Demo;Component/Images/Test.jpg"/>

 

2、动态绑定

<Grid Grid.Row="1">
      <Image Source="{Binding Path=LTEModel.ImgSource,Converter={StaticResource MyImageConverter}}" Stretch="Fill">
      </Image>
</Grid>  

 

public class LTEModel : BaseModel
    {
        private string _imageSource = null;
        public string ImgSource
        {
            get
            {
                return _imageSource;
            }
            set
            {
                if (value != _imageSource)
                {
                    _imageSource = value;
                    FirePropertyChanged("ImgSource");
                }
            }
        }
    }  

 

Converters.cs

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
   }  

 

参考链接:https://www.cnblogs.com/seekdream/p/5277237.html

标签:object,return,string,Image,绑定,value,WPF,imageSource,public
来源: https://www.cnblogs.com/Mars-0603/p/15357357.html