0

I am having issues with populating a listview in WPF.
The problem I am facing is exactly the same as Bind an ObservableCollection to a ListView.
But the solution is not working for me. I am still getting Error: 40 : BindingExpression path error with the binded properties I have in my xaml file.
My xaml is as follows:

<ListView x:Name="qList" ItemsSource="{Binding Question}">
 <ListView.View>
  <GridView>
   <GridViewColumn Header="h1" DisplayMemberBinding="{Binding qNo}"/>
   <GridViewColumn Header="h2" DisplayMemberBinding="{Binding question}"/>
   <GridViewColumn Header="h3" DisplayMemberBinding="{Binding imageName}"/>
   <GridViewColumn Header="h4" DisplayMemberBinding="{Binding a1}"/>
   <GridViewColumn Header="h5" DisplayMemberBinding="{Binding a2}"/>
   <GridViewColumn Header="h5" DisplayMemberBinding="{Binding a3}"/>
   <GridViewColumn Header="h6" DisplayMemberBinding="{Binding a4}"/>
   <GridViewColumn Header="h7" DisplayMemberBinding="{Binding ca}"/>
  </GridView>
 </ListView.View>
</ListView>

In my .cs file I have defined the ObservableCollection and Question class as follows:

ObservableCollection<Question> _Questions = new ObservableCollection<Question>();

public ObservableCollection<Question> Questions
{ get { return _Questions; } }

public class Question
{
 public int qNo { get; set; }
 public string question { get; set; }
 public string imageName { get; set; }
 public string a1 { get; set; }
 public string a2 { get; set; }
 public string a3 { get; set; }
 public string a4 { get; set; }
 public int ca { get; set; }
}

In order to populate the listview (qList) I am using the following code:

DataSet ds = db.GetQList(qSetNo); //This is returning a correct dataset
_Questions.Add(new Question { });

if (ds.Tables.Count > 0)
 qList.ItemsSource = ds.Tables[0].DefaultView;

The listview is being populating with correct data. This is noticed as relevant textboxes are populated with data from the dataset (ds) as the listview rows are clicked.

Edit:

new SqlDataAdapter(new SqlCommand("SELECT " + q_column_qNo + "," + q_column_question + "," + q_column_imageName + "," + q_column_a1 + "," + q_column_a2 + "," + q_column_a3 + "," + q_column_a4 + "," + q_column_ca + " FROM " + q_table_name + " WHERE " + q_column_qSet + "=" + qSetNo, conn)).Fill(ds);
Community
  • 1
  • 1

1 Answers1

0

this could be a real simple one; your binding: {Binding Question}

and your C#: Questions

(Plural vs. Singular)

Been there myself before :)

slimbofat
  • 391
  • 2
  • 7
  • I wish that was the case. I have 2 other correctly working listviews in which the naming convention is similar (the binding and the public class are named the same). And changing the binding to Questions (plural) did not work. – user4618497 Jul 11 '15 at 06:32
  • I think you're missing a conceptual piece here... the name of the public class doesn't matter here at ALL. You're binding to the PROPERTY. So ain't nothing gonna work as you've posted it. That being said, there's something probably incorrect in how you're marrying your view to your ViewModel as well (which there's no way to tell from what yo've posted.) – slimbofat Jul 11 '15 at 17:18
  • I have tried to bind the listview itemsource to Question, Questions and _Questions, neither is working. I have also modified the data set to only get the values used in the gridivewcolums (updated in the question). – user4618497 Jul 11 '15 at 20:42
  • First, rest assured that `Questions` is what you want in the binding. When you say ".cs File", is that your code-behind or a seperate class (viewmodel)? – slimbofat Jul 11 '15 at 21:02
  • The files are named as "New questions.xaml" (which is a UserControl) and "New questions.xaml.cs", so it is my code-behind (I believe). – user4618497 Jul 11 '15 at 21:07
  • OK, I solved it. I renamed the bindings in the GridViewColumns and the Question class to exactly match the database column name. – user4618497 Jul 11 '15 at 21:21
  • Hm, glad you've solved it, but that's a pretty unfulfilling fix; WPF data binding has nothing directly to do with database access. I also don't think you should be setting the ItemsSource value in code-behind, especially if you're already binding to it. You really just want to change the PROPERTY it binds to (in this case Questions). This is the whole point of Data-Binding; you don't interact directly with the UI. Sorry for the tome, I just think this fundamental concept is your problem and will help you significantly in the future. – slimbofat Jul 11 '15 at 22:08