• 當前位置:首頁 > IT技術 > Windows編程 > 正文

    【WPF】Image綁定圖片
    2021-09-30 17:03:37

    一、靜態綁定

    <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

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码