如何在 WPF 中显示表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22280964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:09:34  来源:igfitidea点击:

How to show a table in a WPF

c#wpfxamldatatable

提问by Emmet

I need help with a simple project I am doing in Visual Studio just to learn coding a little bit. I would like to show a table containing objects from a class called Car in a WPF, and this table should be done programmatically.

我需要帮助我在 Visual Studio 中做一个简单的项目,只是为了学习一点编码。我想显示一个表,其中包含来自 WPF 中名为 Car 的类的对象,并且该表应该以编程方式完成。

So, I added a ListView in MainWindow.xaml and set the name for GridView to GridView1

因此,我在 MainWindow.xaml 中添加了一个 ListView 并将 GridView 的名称设置为 GridView1

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView HorizontalAlignment="Center" Height="100" Margin="0,10,0,0" VerticalAlignment="Top" Width="100">
            <ListView.View>
                <GridView x:Name="GridView1">
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

In the MainWindow.xaml.cs I did this

在 MainWindow.xaml.cs 我做了这个

using System;
...
//Add this to use DataTable
using System.Data;

namespace Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //create some objects
            Car ferrari = new Car("Ferrari","Front-Wheel");
            Car mercedes = new Car("Mercedes","Rear-Wheel");

            //create a DataTable
            DataTable dt = new DataTable();
            //add 2 columns
            dt.Columns.Add("Vehicle Name");
            dt.Columns.Add("Vehicle Drive");
            //add 2 rows
            dt.Rows.Add(ferrari.Name, ferrari.Drive);
            dt.Rows.Add(mercedes.Name, mercedes.Drive);
            //bind it to GridView
            GridView1.DataSource = dt;
            GridView1.DataBind();          
        }
    }
}

The problem are the last 2 lines, Visual Studio says it does not contain a definition nor extension method for 'DataSource' and 'DataBind', but all the examples I saw online make use of them. Where is the mistake?

问题是最后两行,Visual Studio 说它不包含“DataSource”和“DataBind”的定义或扩展方法,但我在网上看到的所有示例都使用了它们。错误在哪里?

采纳答案by cvraman

Change the xaml so that it looks like this :

更改 xaml 使其看起来像这样:

    <ListView HorizontalAlignment="Center" Height="100" Margin="0,10,0,0" 
              VerticalAlignment="Top" Width="250" ItemsSource="{Binding dt}">
        <ListView.View>
            <GridView x:Name="GridView1">
                <GridViewColumn DisplayMemberBinding= "{Binding Path=Name}" 
                                Header="Vehicle Name" Width="100"/>
                <GridViewColumn DisplayMemberBinding= "{Binding Path=Drive}" 
                                Header="Vehicle Drive" Width="100"/>
            </GridView>
        </ListView.View>
    </ListView>

Change your code behind :

更改后面的代码:

using System.Windows;
using System.Windows.Controls;
namespace SOWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var carViewModel = new CarViewModel();
            carViewModel.LoadCars();
            this.DataContext = carViewModel;
        }
    }
}

Add a View Model and instead of using a datatable, you are using an ObservableCollection.

添加一个视图模型,而不是使用数据表,您使用的是 ObservableCollection。

using System.Collections.ObjectModel;
using System.Data;
namespace SOWPF
{
    public class CarViewModel
    {
        public ObservableCollection<Car> dt { get; set; }
        public void LoadCars()
        {
            dt = new ObservableCollection<Car>();
            Car ferrari = new Car("Ferrari", "Front-Wheel");
            Car mercedes = new Car("Mercedes", "Rear-Wheel");
            dt.Add(ferrari);
            dt.Add(mercedes);
        }

    }

    public class Car
    {
        public Car(string name, string drive)
        {
            Name = name;
            Drive = drive;
        }
        public string Name { get; set; }
        public string Drive { get; set; }
    }
}