C# 在按钮中显示来自类属性的图像

C# 在按钮中显示来自类属性的图像,c#,wpf,C#,Wpf,我需要在一个按钮中显示一个来自类属性的图像,绑定不起作用 我是这样做的: 我有一个Products类,它包含system.drawable.image类型的ProductImage和一些其他字符串 XAML代码: <Button Content="{Binding ProductImage}" Name="ImageButton"></Button> 该按钮显示内容:System.Drawing.Bitmap 请帮忙 编辑:我发现添加Window2.XAML.cs文件很

我需要在一个按钮中显示一个来自类属性的图像,绑定不起作用

我是这样做的:

我有一个Products类,它包含system.drawable.image类型的ProductImage和一些其他字符串

XAML代码:

<Button Content="{Binding ProductImage}" Name="ImageButton"></Button>
该按钮显示内容:System.Drawing.Bitmap

请帮忙

编辑:我发现添加Window2.XAML.cs文件很重要

MySqlConnection connection = new MySqlConnection(ConnectionManager.ConnectionString);
        try
        {
            connection.Open();
            MySqlCommand command = new MySqlCommand("SELECT * FROM products", connection);
            MySqlDataReader reader = command.ExecuteReader();
            var list = new List<ProductsBLL>();
            while (reader.Read())
            {
                ProductsBLL product = new ProductsBLL();

                product.ProductId = (int) reader[0];
                product.ProductName = (string) reader[1];
                product.ProductReference = (string) reader[2];
                product.ProductColor = (string) reader[3];
                var imagems = (byte[]) reader[4];
                MemoryStream ms = new MemoryStream(imagems);
                product.ProductImage = System.Drawing.Image.FromStream(ms);
                product.ProductDescription = (string) reader[5];
                product.ProductUnitPrice = (decimal) reader[6];
                product.ProductUnitInStock = (int) reader[7];
                product.ProductUnitInCommand = (int) reader[8];

                list.Add(product);
                product = null;
            }

            ProductsListView.ItemsSource = list;
        }
        catch (Exception e)
        {
            MessageBox.Show(this, e.Message);

        }
        finally
        {
            if(connection.State == ConnectionState.Open)
                connection.Close();
        }
尝试将图像控件用作位图的占位符:

ProductImage属性的类型当前为System.Drawing.Image,它是WinForms,而不是WPF。WPF图像控件无法直接绘制System.Drawing.Image

应将属性类型更改为:

现在,您可以创建System.Windows.Media.Imaging.BitmapImage或的实例 System.Windows.Media.Imaging.BitmapFrame作为属性的值。请注意,必须设置BitmapCacheOption.OnLoad,因为您希望在加载图像后立即处理流

byte[] imageBuffer = ...

using (var memoryStream = new MemoryStream(imageBuffer))
{
    product.ProductImage = BitmapFrame.Create(
        memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}

最后,如另一个答案中所述,您必须使用图像控件作为按钮的内容:


我已经这样做了,图像没有显示在XAML代码中我有一个“无法解析symbole ProductImage”错误,我应该做的是XAML.cs文件吗?在class.XAML.cs文件中,我只创建产品实例,并从数据库中为ProductImage分配一个图像。我现在无法测试代码,但如果绑定到ProductImage时没有任何错误,比如您所发布的问题,我不明白为什么会有这样的错误,现在无法解决symbol ProductImage错误..您可以检查我的编辑,我添加了我的cs文件。从数据库检索数据后,我需要在listview中显示图像,并在按钮中显示每个图像,如果你能帮忙的话。我能得到一些帮助吗:/
public ImageSource ProductImage { get; set; }
byte[] imageBuffer = ...

using (var memoryStream = new MemoryStream(imageBuffer))
{
    product.ProductImage = BitmapFrame.Create(
        memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
using (var memoryStream = new MemoryStream(imageBuffer))
{
    var bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = memoryStream;
    bitmapImage.EndInit();
    product.ProductImage = bitmapImage;
}
<Button Name="ImageButton">
    <Image Source="{Binding ProductImage}" />
</Button>