将图像添加到DataGridview

时间:2014-11-03 14:36:59

标签: c# wpf image datagrid

我在Show Image In dataGrid阅读了文章,但我不确定如何添加图片。我正在关注图像的Windows 7触摸屏开发工具包示例,并希望将图像放在数据网格中以便它们可以滚动(示例将它们放在画布上的圆圈中)。 因此,在图像路径中添加图像时,只需将其放在字符串数组中:

private string[] GetPictureLocations()
{
    string[] pictures = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.jpg");

    // If there are no pictures in MyPictures
    if (pictures.Length == 0)
        pictures = new string[] 
                    { 
                        @"images\Pic1.jpg", 
                        @"images\Pic2.jpg", 
                        @"images\Pic3.jpg",
                        @"images\Pic4.jpg" 
                    };
    return pictures;
}

//Load pictures to the canvas
private void LoadPictures()
{
    string[] pictureLocations = GetPictureLocations();
    double angle = 0;
    double angleStep = 360 / pictureLocations.Length;
    foreach (string filePath in pictureLocations)
    {
        try
        {
            Picture p = new Picture();
            p.ImagePath = filePath;
            p.Width = 300;
            p.Angle = 180 - angle;
            double angleRad = angle * Math.PI / 180.0;
            p.X = Math.Sin(angleRad) * 300 + (_canvas.ActualWidth - 300) / 2.0;
            p.Y = Math.Cos(angleRad) * 300 + (_canvas.ActualHeight - 300) / 2.0;
            _canvas.Children.Add(p);
            angle += angleStep;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Trace.WriteLine("Error:" + ex.Message);
        }
    }
}

堆栈溢出文章中的示例是:

DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "MyHeader";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Image));
Binding b1 = new Binding("Picture");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
datagrid.Columns.Add(col1);

我不确定如何合并这两个,以便我可以在datagrid中显示加载的图像(p)。或者有更简单的方法吗?

0 个答案:

没有答案