System.Windows.Controls.ListView和System.Windows.Forms.ListView之间的区别?

时间:2018-03-02 14:52:35

标签: c# wpf winforms

我是noob .NET程序员,所以我尝试使用ListView并显示我从数据库中读取的项目。我尝试创建列标题,但System.Windows.Controls.ListView中没有属性Column,它位于Forms.ListView中。

有人可以告诉我这是差异吗?我应该在哪里使用?

我应该使用Controls.ListView还是应该在WPF中使用Forms.ListView?

我希望我的应用程序是完整的WPF开发并避免使用WinForms。我希望能够完全控制GUI界面,并且不需要用代码和数学来调整WinForms的大小。

最后我只想显示一个来自数据库的只读表数据,它应该能够调整到全屏和向后,使用滚动条等...

2 个答案:

答案 0 :(得分:0)

System.Windows.Forms来自WinForms,System.Windows.Controls来自WPF,因此您应该使用System.Windows.Controls.ListView

答案 1 :(得分:0)

为了在WPF应用程序中显示数据库中的只读表数据,您必须使用ItemSource。我将采用具有ID和名称属性的User实体的简单示例。

  1. 在XAML视图中,我们添加了ListView标记

    <ListView x:Name="UserTableListView"/>

  2. 然后我们需要创建用户类(实体)

    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. 然后我们创建一个用数据填充ListView的方法

    /// <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. 我们创建ListView的列,然后调用方法&#34; FillUsersListView()&#34;填写数据

        //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!
    
  5. 快乐编码