I'm new to WPF and want to do some basic databinding.
I have a List of a CustomObject and want to bind it to a DataGrid.
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<ArticleItem> list = new List<ArticleItem>()
{
new ArticleItem(){ ID=3, Title="test", ViewCount=5},
new ArticleItem(){ ID=3, Title="test", ViewCount=5},
new ArticleItem(){ ID=3, Title="test", ViewCount=5},
new ArticleItem(){ ID=3, Title="test", ViewCount=5},
};
}
}
public class ArticleItem
{
public int ID { get; set; }
public int ViewCount { get; set; }
public String Title { get; set; }
}
}
This is how my grid looks like:
<DataGrid Height="179" HorizontalAlignment="Left" Margin="54,65,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="382">
<DataGrid.Columns>
<DataGridTextColumn Header="ID"/>
<DataGridTextColumn Header="ViewCount" />
<DataGridTextColumn Header="Title" />
</DataGrid.Columns>
</DataGrid>
I'm used to the databinding from ASP.Net, where I can easily say:
this.dataGrid1.DataSource = list;
How must I proceed in WPF?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…