Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Binding doesn't show ObservableCollection

Why is my listview, empty, while ‘Klanten’ has objects?
I have removed the toString in the object class also.

screen

c# constructor

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    public partial class Klantbeheerder: Window {
        public ObservableCollection < Klant > Klanten;
        private KlantManager _manager;
        public Klantbeheerder() {
          InitializeComponent();
          _manager = new(new KlantRepo(ConfigurationManager.ConnectionStrings["connStr"].ToString()));
          Klanten = _manager.GeefKlanten();
          DataContext = Klanten;
        }

Window

    d:DataContext="{d:DesignInstance local:Klantbeheerder, IsDesignTimeCreatable=True}"

Inside Grid

<ListView
   x:Name="lstvKlanten"
   Height="270"
   Margin="0,15,15,15"
   HorizontalAlignment="Left"
   ItemsSource="{Binding Klanten}"
   SelectionMode="Single">
   <ListView.View>
      <GridView>
         <GridViewColumn
            Width="50"
            DisplayMemberBinding="{Binding Id}"
            Header="Id" />
         <GridViewColumn
            Width="150"
            DisplayMemberBinding="{Binding Naam}"
            Header="Naam" />
         <GridViewColumn
            Width="100"
            DisplayMemberBinding="{Binding Adres}"
            Header="Adres" />
      </GridView>
   </ListView.View>
</ListView>

>Solution :

Why you use ListView for translate grid? Use DataGrid, it’s simple:

MainWindow.xaml:

<DataGrid ItemsSource="{Binding Products}" AutoGenerateColumns="False">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Product"  Binding="{Binding Name}" />
      <DataGridTextColumn Header="Price" Binding="{Binding Price}" />
   </DataGrid.Columns>
</DataGrid>

MainWindow.xaml.cs:

//LOOK, THIS IS PROPERTY!!
public ObservableCollection<Product> Products { get; set; }
public MainWindow()
{
    InitializeComponent();
    Products = new ObservableCollection<Product>();
    Products.Add(new Product { Name = "Apple", Price = 1 });
    Products.Add(new Product { Name = "Banana", Price = 2 });

    DataContext = this;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading