原来Image控件的Source属性是一个ImageSource对象啊!

  <StackPanel>
    <StackPanel.Resources>
      <local:ImageConverter x:Key="cvt_image" />
    </StackPanel.Resources>
    <TextBox x:Name="txtPath" Text="E:\网盘\我的酷盘\MyFolder\MyPhoto\Panda.jpg" />
    <Image Source="{Binding ElementName=txtPath, Path=Text}"/>
    <Image Source="{Binding ElementName=txtPath, Path=Text, Converter={StaticResource cvt_image}}" />
  </StackPanel>

 
这里只是一个示范,其实是可以将数据库中取出来的二进制数据,直接转换成一个BitmapImage对象返回的:

    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new BitmapImage(new Uri(value.ToString()));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


 

以前不知道,一直以为只能放一个图片的地址,所以一直不知道怎么把Bitmap对象直接放进去,今天总算知道,他原来是一个ImageSource类型,而继承该类型的class有:BitmapImage (继承自BitmapSource)和DrawingImage(继承自ImageSource)。BitmapImage可以显示一个普通位图,DrawingImage可以显示矢量图。

。。。XAML真是害人啊!唉,介绍WPF纯代码技术的书实在太少了

原文地址:https://www.cnblogs.com/puncha/p/3876998.html