-2

I am noob .NET programmer so I tried to use ListView and display my items which I read from database. I tried to create column headers but the property Column is not available in System.Windows.Controls.ListView and it is in Forms.ListView.

Can somebody tell me that is the difference and which should I use where?

Should I use Controls.ListView or should I use Forms.ListView in WPF?

I want my app to be full WPF developed and avoid using WinForms. I want to have full control of the GUI interface and don't bother resizing WinForms with code and maths.

In the end I just want to display a read-only table data from database and it should be able to resize to full screen and backwards, with scrollbars, etc...

Vlad
  • 2,739
  • 7
  • 49
  • 100
  • 5
    Everything in System.Windows.Forms is WinForms and shouldn't be used in a WPF app. – Clemens Mar 02 '18 at 14:53
  • Yes, indeed a very basic question. I was in a hurry back then. I would delete the question but there are two answers. I will try to add more info maybe to make it specific to some sort. – Vlad Mar 06 '18 at 08:00

2 Answers2

1

In order to display a read-only table data from database in your WPF application you must use ItemSource.I will take a simple example of User entity with ID and Name proprety.

  1. In the XAML view we add the ListView tag

    <ListView x:Name="UserTableListView"/>

  2. Then we need to create the User Class(Entity)

    public class User
    {
      public int ID { get; set; } //Id Field with getter and setter
      public string Name { get; set; }//Name Field with getter and setter
    }
    
  3. Then we create a method to fill our ListView with data

    /// <summary>
    /// This method fill our listview with the list of users  
    /// </summary>
    private void FillUsersListView()
    {
    
        //We take an example of creating 3 users 
        User user1 = new User { ID = 1, Name = "Bettaieb" };
        User user2 = new User { ID = 2, Name = "Jhon" };
        User user3 = new User { ID = 3, Name = "Alex" };
    
        //We create a user list to use it later on the listview
        List<User> user_list = new List<User>();
        //We add the 3 users to the user_list
        user_list.Add(user1);
        user_list.Add(user2);
        user_list.Add(user3);
        //Finnaly we set the itemsource of ListView
        UserTableListView.ItemsSource = user_list;
    }
    
  4. We create columns of our ListView and we call the method "FillUsersListView()" to fill the data

        //We add the columns it must be similair to our User class
        gridView.Columns.Add(new GridViewColumn
        {
            Header = "Id", //You can set here the title of column
            DisplayMemberBinding = new Binding("ID") //The Biding value must be matched to our User Class proprety ID
        });
        gridView.Columns.Add(new GridViewColumn
        {
            Header = "Name",
            DisplayMemberBinding = new Binding("Name")//The Biding value must be matched to our User Class proprety Name
        });
        FillUsersListView(); //We call here the method in order to fill our listview with data!
    

Happy Coding

0

System.Windows.Forms is from WinForms and System.Windows.Controls is from WPF, so you should use System.Windows.Controls.ListView.

kwyntes
  • 1,045
  • 10
  • 27