0

I have read the article at Show Image In dataGrid bu t am un sure of how to add the image. I am following the Windows 7 touch screen development kit example for images and would like to place the images in a data grid so they can scroll (the example has them in a circle on the canvas). So, when adding an image in the image paths are just placed in a string array:

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);
        }
    }
}

The example from the stack overflow article is:

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);

I am unsure how to consolidate the two so I can show the loaded images (p) in the datagrid. Or is there an easier way?

Community
  • 1
  • 1
rigamonk
  • 1,179
  • 2
  • 17
  • 45

0 Answers0