0

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? ):

4

1 回答 1

0

我注意到了几件事..

  1. 看起来您的类 _Images 未声明为公共

  2. 当调用该类的构造函数时(当您将其添加为 Window.DataContext 时发生,CarregaImagens() 是如何/在哪里被调用的。它是否被调用过?如果没有,请放入默认构造函数。

  3. 你不应该用下划线命名一个类(这是私有成员的约定)你知道为了在 xaml 中绑定,属性不能是私有的?

希望这些帮助...

于 2012-04-04T20:20:22.103 回答