使用 XAML 将 System.Drawing.Image 绑定到 System.Windows.Image 控件 [英] using XAML to bind to a System.Drawing.Image into a System.Windows.Image control
问题描述
我将一个 ListView 绑定到一个对象列表,就像这样;
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; }
我想填充一个 DataTemplate
但我不知道如何;如果我在模板中执行此操作;
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')
推荐答案
找到了一种令我满意的方式.使用 Reed Copsey 的指针 和 本教程 我已将代码包装为 IValueConverter
.
Found a way I'm happy with. Using Reed Copsey's pointer and this tutorial I've wrapped the code as a IValueConverter
.
这是从System.Drawing.Image
到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;
}
}
}
然后需要将图片转换器作为资源导入XAML;
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>
然后你可以在XAML中使用它直接绑定到Image,使用新的转换器;
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屋!