【问题标题】:Difference between System.Windows.Controls.ListView and System.Windows.Forms.ListView? [duplicate]System.Windows.Controls.ListView 和 System.Windows.Forms.ListView 的区别? [复制]
【发布时间】:2018-03-02 14:52:34
【问题描述】:

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

有人能告诉我这有什么区别吗?我应该在哪里使用哪个?

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

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

最后我只想显示数据库中的只读表数据,它应该能够调整到全屏和向后,带有滚动条等......

【问题讨论】:

  • System.Windows.Forms 中的所有内容都是 WinForms,不应在 WPF 应用程序中使用。
  • 是的,确实是一个非常基本的问题。那时我很着急。我会删除这个问题,但有两个答案。我会尝试添加更多信息以使其特定于某种类型。

标签: c# wpf winforms


【解决方案1】:

为了在您的 WPF 应用程序中显示来自数据库的只读表数据,您必须使用 ItemSource。我将举一个带有 ID 和 Name 属性的 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 的列并调用方法“FillUsersListView()”来填充数据

        //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!
    

快乐编码

【讨论】:

    【解决方案2】:

    System.Windows.Forms 来自 WinForms,System.Windows.Controls 来自 WPF,所以你应该使用System.Windows.Controls.ListView

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 2017-03-31
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2016-02-10
      • 2019-05-26
      • 2015-11-07
      • 2011-03-05
      相关资源
      最近更新 更多