使用XAML绑定到System.Drawing.Image对象变成System.Windows.Image控制 [英] using XAML to bind to a System.Drawing.Image into a System.Windows.Image control

查看:476
本文介绍了使用XAML绑定到System.Drawing.Image对象变成System.Windows.Image控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一个ListView绑定到对象的列表,像这样;

 < ListView控件
    的ItemsSource ={绑定路径=}
    的ItemTemplate ={DynamicResource EventTemplate}>

我绑定到该声明两个属性的对象;

 字符串显示名称{搞定; }
System.Drawing.Image对象图像{搞定;组; }

我想填充的DataTemplate ,但我无法弄清楚如何;如果我这样做,我的模板;

 < StackPanel的方向=横向>
    <图像来源={绑定路径=图片}/>
    < TextBlock的文本={绑定路径=显示名称}/>
< / StackPanel的>

文本出现,但图像没有。我究竟做错了什么?调试输出显示

  System.Windows.Data错误:1:无法创建默认的转换器
执行类型之间的'单向'的转换
System.Drawing.Image对象和System.Windows.Media.ImageSource。
考虑使用绑定的转换器属性。
BindingEx pression:路径=图片;的DataItem ='RealElement
(哈希code = 54308798);目标元素是'图像'(名称='');
target属性是源(类型'的ImageSource')


解决方案

找到了一种方法我很高兴。使用<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/833ca60f-6a11-4836-bb2b-ef779dfe3ff0/\">Reed •科普塞的指针和本教程我包裹code作为的IValueConverter

下面是从为System.Drawing.Image 来转换器 System.Windows.Media.ImageSource ;

 使用系统;
使用System.Drawing.Imaging;
使用System.Globalization;
使用System.IO;
使用System.Windows.Data;命名空间System.Windows.Media
{
    ///&LT;总结&gt;
    ///单向转换器从System.Drawing.Image对象到System.Windows.Media.ImageSource
    ///&LT; /总结&gt;
    [ValueConversion(typeof运算(System.Drawing.Image对象)的typeof(System.Windows.Media.ImageSource))]
    公共类ImageConverter:的IValueConverter
    {
        公共对象转换(对象的值,类型TARGETTYPE,
            对象参数,CultureInfo的文化)
        {
            //空图像空...
            如果(价值== NULL){返回NULL; }            VAR图像=(System.Drawing.Image对象)值;
            //图像的WinForms我们想从WPF图像...
            VAR位=新System.Windows.Media.Imaging.BitmapImage();
            bitmap.BeginInit();
            MemoryStream的MemoryStream的=新的MemoryStream();
            //保存到内存流...
            image.Save(MemoryStream的,ImageFormat.Bmp);
            //倒带流...
            memoryStream.Seek(0,System.IO.SeekOrigin.Begin);
            bitmap.StreamSource = MemoryStream的;
            bitmap.EndInit();
            返回位图;
        }        公共对象ConvertBack(对象的值,类型TARGETTYPE,
            对象参数,CultureInfo的文化)
        {
            返回null;
        }
    }
}

然后,你需要把图像转换成XAML作为一种资源;

 的xmlns:医学版=CLR的命名空间:System.Windows.Media
...&LT; ListView.Resources&GT;
    &LT;配有:ImageConverter X:键=imageConverter/&GT;
&LT; /ListView.Resources>

然后,你可以用它在XAML直接绑定到图像,使用新的转换器;

 &lt;图像来源={绑定路径=图像,转换器= {StaticResource的imageConverter}}/&GT;

I'm binding a ListView to a list of objects, like so;

<ListView 
    ItemsSource="{ Binding Path=. }"
    ItemTemplate="{DynamicResource EventTemplate}">   

I'm binding to an object which declares two properties;

string DisplayName { get; }
System.Drawing.Image Image { get; set; }

I want to populate a DataTemplate but I can't figure out how; if I do this in my template;

<StackPanel Orientation="Horizontal">
    <Image Source="{ Binding Path=Image }" />
    <TextBlock Text="{ Binding Path=DisplayName }" />
</StackPanel>      

The text appears but the image does not. What am I doing wrong? The debug output shows

System.Windows.Data Error: 1 : Cannot create default converter
to perform 'one-way' conversions between types
'System.Drawing.Image' and 'System.Windows.Media.ImageSource'.
Consider using Converter property of Binding.
BindingExpression:Path=Image; DataItem='RealElement'
(HashCode=54308798); target element is 'Image' (Name='');
target property is 'Source' (type 'ImageSource')

解决方案

Found a way I'm happy with. Using Reed Copsey's pointer and this tutorial I've wrapped the code as a IValueConverter.

Here's the converter from System.Drawing.Image to System.Windows.Media.ImageSource;

using System;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows.Data;

namespace System.Windows.Media
{
    /// <summary>
    /// One-way converter from System.Drawing.Image to System.Windows.Media.ImageSource
    /// </summary>
    [ValueConversion(typeof(System.Drawing.Image), typeof(System.Windows.Media.ImageSource))]
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            // empty images are empty...
            if (value == null) { return null; }

            var image = (System.Drawing.Image)value;
            // Winforms Image we want to get the WPF Image from...
            var bitmap = new System.Windows.Media.Imaging.BitmapImage();
            bitmap.BeginInit();
            MemoryStream memoryStream = new MemoryStream();
            // Save to a memory stream...
            image.Save(memoryStream, ImageFormat.Bmp);
            // Rewind the stream...
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            bitmap.StreamSource = memoryStream;
            bitmap.EndInit();
            return bitmap;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}

Then you need to bring the image converter into XAML as a resource;

xmlns:med="clr-namespace:System.Windows.Media"
...

<ListView.Resources>
    <med:ImageConverter x:Key="imageConverter" />
</ListView.Resources>

Then you can use it in XAML to bind directly to the Image, using the new converter;

<Image Source="{ Binding Path=Image, Converter={StaticResource imageConverter} }" />

这篇关于使用XAML绑定到System.Drawing.Image对象变成System.Windows.Image控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆