Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting a WPF Listview to display ObservableCollection<T> using databinding
    primarykey
    data
    text
    <p>I have a observable collection of type Project, that I want to be displayed in a ListView but nothing is added to my ListView which I really dont understand</p> <p>My MainWindow.xaml</p> <pre><code> &lt;ListView Name="ListViewProjects" Grid.Column="0" Grid.RowSpan="3" SelectionChanged="ListViewProjectsSelectionChanged" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" MinWidth="100"&gt; &lt;ListView.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;WrapPanel&gt; &lt;TextBlock Text="{Binding Path=ProjectID}"/&gt; &lt;TextBlock Text="{Binding Path=ProjectName}"/&gt; &lt;/WrapPanel&gt; &lt;/DataTemplate&gt; &lt;/ListView.ItemTemplate&gt; &lt;/ListView&gt; </code></pre> <p>My MainWindow.cs</p> <pre><code> public partial class MainWindow : Window { ObservableCollection&lt;Project&gt; Projects = new ObservableCollection&lt;Project&gt;(); ObservableCollection&lt;Employee&gt; Employees = new ObservableCollection&lt;Employee&gt;(); public MainWindow() { InitializeComponent(); DataContext = Projects; Project pro1 = new Project(1, "Swordfish"); Projects.Add(pro1); Employee empMads = new Employee("Mads", 1); Employee empBrian = new Employee("Brian", 2); Employees.Add(empMads); Employees.Add(empBrian); } private void ListViewProjectsSelectionChanged(object sender, SelectionChangedEventArgs e) { } } </code></pre> <p>and my Project.cs which is the class file</p> <pre><code>[Serializable] class Project : INotifyPropertyChanged { public Project(int id, string name) { ID = id; Name = name; } private int id; public int ID { get { return id; } set { id = value; NotifyPropertyChanged("ProjectID"); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("ProjectName"); } } [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } </code></pre> <p>But nothing is added to my list I cant see what I am missing for it to work.</p> <p>I have it as a observablecollection I do DataContext = the collection And I do binding in the xaml file</p> <p>Edit codepart 1:</p> <pre><code> public ObservableCollection&lt;Project&gt; Projects { get; set; } public ObservableCollection&lt;Employee&gt; Employees { get; set; } public MainWindow() { InitializeComponent(); Projects = new ObservableCollection&lt;Project&gt;(); Employees = new ObservableCollection&lt;Employee&gt;(); DataContext = Projects; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload