0

What I am trying to do is simply have a combobox populate with data from an sqlite table. While I have done this with code methods, I wold really like to do this in what I can see is the better WPF way to do things.

From what I understand the flow should go something like this:

I should have a class that holds the data, I made a quick class which the default constructor is to connect to the database and dump it's results to a list like so:

internal class mainmenusql
{
    private List<string> _Jobs;

    public mainmenusql()
    {
        SQLiteConnection conn = new SQLiteConnection();
        conn.ConnectionString = "Data Source=C:\\Users\\user\\Documents\\db.sqlite;Version=3";

        try
        {
            conn.Open();
            SQLiteDataReader reader;
            SQLiteCommand command = new SQLiteCommand(conn);
            command.CommandType = CommandType.Text;
            command.CommandText = "SELECT * FROM Tasks";
            reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    _Jobs.Add(reader.GetValue(0).ToString());
                }
            }
            else
            {
                MessageBox.Show("No records");
            }

        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
        finally
        {
            conn.Close();
        }
    }
}

Having some errors with the list "Object reference not set to an instance of an object".

But anyways, the next step should be to set the DataContext of the form to this object right?

    public MainWindow()
    {
        DataContext = new mainmenusql();
        InitializeComponent();
    }

And finally the combobox should have a binding right?

<Window x:Class="sqliteDatacontext.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox DataContext="{Binding Path=_Jobs}" HorizontalAlignment="Left" Margin="141,124,0,0" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

What am I doing wrong here?

Marc
  • 12,706
  • 7
  • 61
  • 97
Herrozerro
  • 1,601
  • 1
  • 22
  • 37
  • your object missing is because you never initialized the "Jobs" list via _Jobs = new List(); THEN you can add to it. – DRapp Jun 13 '13 at 00:08
  • To add to what @DRapp said, you should also make it Public for it to be accessible as part of the DataContext. – Roberto Hernandez Jun 13 '13 at 00:16
  • okay, that fixed the issue with the list, but i'm still not getting anything in the combobox, should i be using a different kind of datacontext other than {Binding Path=_Jobs}? – Herrozerro Jun 13 '13 at 00:23

1 Answers1

3

For binding to something of a datacontext, it needs to be exposed via public getter / setter...

public class mainmenusql
{
    public List<string> _Jobs
    { get ; protected set; }   


    public mainmenusql()
    {
       _Jobs = new List<string>();

       // rest of populating your data
    }
}

The binding in your window control is the ItemsSource

<ComboBox ItemsSource="{Binding Path=_Jobs}" />

The "DataContext" is applied to the entire window... From that being the basis, any of your controls can have their element "bound" to almost anything "publicly" available ON your data context... in this case, a ComboBox list of choices comes from its "ItemSource" property... So you want the ITEMSOURCE pointing to your _Jobs.

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • Doing this gave me the old issue of "Object reference not set to an instance of an object". Adding the = new ... give me errors in the member declaration. Any Ideas? – Herrozerro Jun 13 '13 at 00:31
  • Thanks! I think we are getting there! But the combo box is giving me "Cannot resolve symbol _Jobs due to unknown datacontext. – Herrozerro Jun 13 '13 at 00:36
  • Thanks! this works perfectly! I'm glad I was at least sorta on the right track, I think I understand this much better now. – Herrozerro Jun 13 '13 at 00:50