Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have been trying to bind the Images in a folder od my desktop to the WPF application and I am stuck in the middle could anyone please help me...

the code pertaining to class model
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageList.Model
{
    public class ImageDetails
    {
        /// <summary>
        /// A name for the image, not the file name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// A description for the image.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Full path such as c:\path\to\image.png
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// The image file name such as image.png
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// The file name extension: bmp, gif, jpg, png, tiff, etc...
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// The image height
        /// </summary>
        public int Height { get; set; }

        /// <summary>
        /// The image width.
        /// </summary>
        public int Width { get; set; }

        /// <summary>
        /// The file size of the image.
        /// </summary>
        public long Size { get; set; }

    }
}


----------------------------------------------------------------------------------------

XAML CODE

<window x:class="ImageList.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
    <grid>
        <itemscontrol name="ImageList" itemssource="{Binding ImageList}">
            <itemscontrol.itemtemplate>
                <datatemplate>
                    <border borderthickness="1" borderbrush="#FFD0D1D7" padding="5" margin="10,10,0,0">
                        <stackpanel orientation="Horizontal">
                            <!--image and dimensions-->
                            <grid width="88" height="55">
                                <Image Source="{Binding Path}"/>
                                <textblock removed="#B2000000" foreground="White" height="16" textalignment="Center" verticalalignment="Bottom">
                                    <textblock.text>
                                        <multibinding stringformat="{}{0}x{1}">
                                            <binding path="Height" />
                                            <binding path="Width" />
                                        </multibinding>
                                    </textblock.text>
                                </textblock>
                            </grid>
                            <!--name, type and size-->
                            <stackpanel orientation="Vertical" margin="5,0,0,0" verticalalignment="Center">
                                <textblock name="ImageName" margin="1" foreground="#FF787878" text="{Binding FileName}" />
                                <textblock name="ImageType" margin="1" foreground="#FF787878">
                                    <textblock.text>
                                        <multibinding stringformat="Type: {0}">
                                            <binding path="Extension" />
                                        </multibinding>
                                    </textblock.text>
                                </textblock>
                                <textblock name="ImageSize" margin="1" foreground="#FF787878">
                                    <textblock.text>
                                        <multibinding stringformat="Size: {0} Bytes">
                                            <binding path="Size" />
                                        </multibinding>
                                    </textblock.text>
                                </textblock>
                            </stackpanel>
                        </stackpanel>
                    </border>
                </datatemplate>
            </itemscontrol.itemtemplate>
        </itemscontrol>
    </grid>
</window>

--------------------------------------------------------------------------------

CODE BEHIND XAML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

using ImageList.Model;

using System.IO;


namespace ImageList
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string[] supportedExtensions = new[] {".png"};
          var files = Directory.GetFiles(System.IO.Path.Combine(root, "Images"), "*.*").Where(s => supportedExtensions.Contains(System.IO.Path.GetExtension(s).ToLower()));
         //   var files = Directory.GetFiles(System.IO.Path.Combine( C:\Users\apmaheshwar\Documents\visual studio 2012\Projects\WpfApplication59\WpfApplication59\ImagesP;
         


            List<ImageDetails> images = new List<ImageDetails>();

            foreach (var file in files)
            {
                ImageDetails id = new ImageDetails()
                {
                    Path = file,
                    FileName = System.IO.Path.GetFileName(file),
                    Extension = System.IO.Path.GetExtension(file)
                };

                BitmapImage img = new BitmapImage();
                img.BeginInit();
                img.CacheOption = BitmapCacheOption.OnLoad;
                img.UriSource = new Uri(file, UriKind.Absolute);
                img.EndInit();
                id.Width = img.PixelWidth;
                id.Height = img.PixelHeight;

             
                FileInfo fi = new FileInfo(file);
                id.Size = fi.Length;
                images.Add(id);
            }

            ImageList.ItemsSource = images;
        }

        private object GetExtension(string s)
        {
            throw new NotImplementedException();
        }
    }
}

---------------------------------------------------------------


Can anyone please help me, I have the submission tomorrow...Please
Posted
Comments
[no name] 27-May-14 8:24am    
First thing is that I do not see anywhere where you are setting your DataContext. And you are attempting to bind to a property "ImageList" that does not exist.
Sergey Alexandrovich Kryukov 27-May-14 12:29pm    
Why binding everything with XAML? XAML for the sake of XAML? Would you better explain what do you want to achieve, in terms of ultimate goals?
—SA
Member 10838492 29-May-14 0:58am    
well the thing is, I want to browse a folder from my desktop and get all the images from that folder into my list box, once I get those images in the list I should be able to drag and drop the selected images from the listbox onto the canvas.
Sergey Alexandrovich Kryukov 29-May-14 1:15am    
Okay, but what's the problem here? And why binding anything to XAML?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900