views:

39

answers:

2

Hi, I'm storing the urls to the images in a sql ce 3.5 database as strings. I want to retrieve the urls and display them in the main application window. Here is the code:

DataSet myDataSet;

        private void OnInit(object sender, EventArgs e)
        {

            string connString = Properties.Settings.Default.SystemicsAnalystDBConnectionString;
            OleDbConnection conn = new OleDbConnection(connString);
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT url FROM Library;", conn);

            myDataSet = new DataSet();
            adapter.Fill(myDataSet, "Library");
            myListBox.DataContext = myDataSet;
        }

The first problem is that I don't think the method onInit is fired. But I don't know the reason for that.

The second problem is with XAML file. I need a container for images (like the listbox for textboxes) and since I won't know how many images are there I need some kind of a template:

                            <DataTemplate>
                                <StackPanel>
                                    <Image Source="{Binding Path=url}" />
                                </StackPanel>
                            </DataTemplate>

But there has to be some kind of a container that would have the datacontext set to the data source.

Could anyone help?

A: 

You can customize a listbox in wpf quite easily to have images in it, instead of text. Use the ItemTemplate or if you want to change to control itself, the ControlTemplate.

Tony
Thanks, Tony. But do you know how to fire the onInit() method? I need to get the data from database when the window opens.
EVA
A: 
<ListBox ItemsSource="{Binding Library}">
  <ListBox.ItemTemplate>
    <DataTemplate>    
         <Image Source="{Binding Path=url}" />
     </DataTemplate>
  </ListBox.ItemTemplate>
 </ListBox>

The DataContext for the ListBox should be your DataSet. You can use OnLoad instead of OnInit

Anyway I dont recommend the DataSet binding, it would be more managable if you create ViewModel class for your Library and create a collection of Library entities

Jobi Joy