Can i bind image to xaml without needing to use image source (the predominant method) which it is (In xaml page):
<Image Height="170" Width="220" Source="{Binding Source=Img}" ></Image>
because i have image converted from byte array and doesn't have any name or source. like here:
Image Img = new Image();
Img=ByteArraytoBitmap(ob0[0].Img.ToArray());
public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
MemoryStream stream = new MemoryStream(byteArray);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
return bitmapImage;
}
You could just add a BitmapImage property on your object (the class of ob0[0]) and then the bitmap image can be directly be binded to the source of the Image (Note that the binding should be Source="{Binding Img}
" and not Source="{Binding Source=Img}"
.
The other solution would for you to create an attached property, something like this should work:
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);
}
}
then to do the binding you can just use it like this:
<Image Height="170" Width="220" local:MyAttachedProperty.ByteArraySource="{Binding Img}" ></Image>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.