WPF ObservableCollection in xaml

Posted by Cloverness on Stack Overflow See other posts from Stack Overflow or by Cloverness
Published on 2010-06-16T10:13:55Z Indexed on 2010/06/16 10:22 UTC
Read the original article Hit count: 231

Filed under:
|

Hi, I have created an ObservableCollection in the code behind of a user control. It is created when the window loads:

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        Entities db = new Entities();
        ObservableCollection<Image> _imageCollection =
       new ObservableCollection<Image>();

        IEnumerable<library> libraryQuery =
        from c in db.ElectricalLibraries

        select c;

        foreach (ElectricalLibrary c in libraryQuery)
        {
            Image finalImage = new Image();
            finalImage.Width = 80;

            BitmapImage logo = new BitmapImage();
            logo.BeginInit();
            logo.UriSource = new Uri(c.url);
            logo.EndInit();

            finalImage.Source = logo;

            _imageCollection.Add(finalImage);

        }

    }

I need to get the ObservableCollection of images which are created based on the url saved in a database. But I need a ListView or other ItemsControl to bind to it in XAML file like this:

But I can't figure it out how to pass the ObservableCollection to the ItemsSource of that control. I tried to create a class and then create an instance of a class in xaml file but it did not work. Should I create a static resource somehow>

Any help will be greatly appreciated.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about observablecollection