在WPF中显示图像datagrid模板字段

时间:2011-08-08 10:10:21

标签: c# wpf linq-to-sql wpfdatagrid

如何使用LINQ to SQL在WPF中的datagrid模板字段中将图像(存储在SQL数据库中)显示到图像控件

1 个答案:

答案 0 :(得分:1)

Linq生成的类是Partial。这允许您扩展它们。因此,假设您的链接类具有以下行的属性:

public byte[] Image {get; set;}

你可以使用像这样的属性添加到部分类

public ImageSource imageSource
{
    get
    {
            var Img = new BitmapImage();
            Img.BeginInit();
            Img.StreamSource = new System.IO.MemoryStream((byte[])Image);
            Img.EndInit();
            return Img;
     }
 }

然后在模板中为您进行模板控制,您只需输入:

<DataGridTemplateColumn Header="Image" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding imageSource}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
相关问题