您可以在对象上添加一个 BitmapImage 属性(ob0[0] 的类),然后位图图像可以直接绑定到 Image 的源(请注意,绑定应该是 Source="{Binding Img}
" 而不是Source="{Binding Source=Img}"
.
另一种解决方案是让您创建一个附加属性,这样的东西应该可以工作:
public class MyAttachedProperty
{
public static readonly DependencyProperty ByteArraySourceProperty =
DependencyProperty.RegisterAttached("ByteArraySource", typeof (Byte[]), typeof (MyAttachedProperty), new PropertyMetadata(default(Byte[],byteArraySource)))
private static void byteArraySource(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Image img = d as Image;
if (e.NewValue != null)
{
img.Source = ByteArraytoBitmap((Byte[]) e.NewValue);
}
}
public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
MemoryStream stream = new MemoryStream(byteArray);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
return bitmapImage;
}
public static void SetByteArraySource(UIElement element, Byte[] value)
{
element.SetValue(ByteArraySourceProperty, value);
}
public static Byte[] GetByteArraySource(UIElement element)
{
return (Byte[]) element.GetValue(ByteArraySourceProperty);
}
}
然后要进行绑定,您可以像这样使用它:
<Image Height="170" Width="220" local:MyAttachedProperty.ByteArraySource="{Binding Img}" ></Image>