To construct the data:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespacewpfapplication2{/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window {Random rnd=NewRandom (); PublicMainWindow () {InitializeComponent (); varTlist =NewList<person>(); for(inti =0; I <Ten; i++) {tlist. ADD (NewPerson () {Name="Zhang San"+I, age= rnd. Next ( -) }); } list= Tlist. Select (x =New{x, id = guid.newguid ()}). (y = y.id). Select (z = z.x). Tolist<person>(); This. DataContext = This; } PublicList<person> List {Get;Set; } } Public classPerson { Public stringName {Get;Set; } Public intAge {Get;Set; } }}
Interface display:
<window x:class="Wpfapplication2.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow"height="850"Width="525"> <StackPanel> <listbox itemssource="{Binding list}"Displaymemberpath="Name"></ListBox> <datagrid name="DG"autogeneratecolumns="False"Itemssource="{Binding list}"Canuseraddrows="False"> <DataGrid.Columns> <datagridtextcolumn binding="{Binding Name}"Width=" -"Header="name"/> <datagridtextcolumn binding="{Binding Age}"Header="Age"/> </DataGrid.Columns> </DataGrid> <textblock text="{Binding Elementname=dg,path=selecteditem.name}"></TextBlock> </StackPanel></Window>
Use of the WPF DataGrid