0
votes

I'm new to WPF and I want to do an app which shows pictures in a ListBox. I made a class called _Images, that gets images from a local folder, puts on a list.

namespace wpfAppSlides.Imagens
{
    class _Images
    {
        public List<Image> imgList = new List<Image>();

        public void CarregaImagens()
        {
            string dir = @"C:\Users\devUser\Img";

            foreach (string file in Directory.GetFiles(dir))
            {
                Image image = new Image();
                BitmapImage source = new BitmapImage();
                source.BeginInit();
                source.UriSource = new Uri(file, UriKind.Relative);
                source.EndInit();
                image.Source = source;

                imgList.Add(image);

            }
        }
    }
}    

An then, in my XAML, I referenced my namespaces.

xmlns:is="clr-namespace:wpfAppSlides._Images"
xmlns:col="clr-namespace:wpfAppSlides"

And put in a DataContext.

<Window.DataContext>
    <is:_Images></is:_Images>
</Window.DataContext>

But when I wanna feed my ListBox with the items on imgList, using ItemsSource (in Properties, not code), he does not find any Path that I can relate to imgList.

ListBox XAML:

<ListBox Height="110" 
             HorizontalAlignment="Left" 
             Margin="14,50,0,0" 
             Name="listBox1" 
             VerticalAlignment="Top" 
             Width="477" ItemsSource="{Binding}" />

MainWindow code:

public partial class MainWindow : Window
{
    _Imagens imgs;
    public MainWindow()
    {
        InitializeComponent();
        imgs = (_Images)this.DataContext;
        imgs.CarregaImagens();

    }
}

I tried to use ObservableCollection, but it's no use.

Help? ):

1

1 Answers

0
votes

I noticed a couple of things..

  1. looks like your class _Images isn't declared as public

  2. When constructor of that class is called (happens when you added it as Window.DataContext, how/where does CarregaImagens() get called. Does it ever get called? If not, put in in the default constructor.

  3. You shouldn't name a class with the underscore (that's the convention for a private member) you know that in order to bind in xaml, the property cannot be private?

hoper these help...