First, it looks like it is after binding,
Form interface code:
1 <DataGrid Name = "FileDataGrid" AutoGenerateColumns = "False" Height = "150" CanUserAddRows = "false"> 2 <DataGrid. columns> 3 <maid Header = "file name" Width = "200 *" IsReadOnly = "True"> 4 <maid. cellTemplate> 5 <DataTemplate> 6 <StackPanel Orientation = "Horizontal"> 7 <TextBlock Text = "{Binding FileName}" Width = "200" Height = "25" verticalignment = "Center "TextAlignment =" Left "/> 8 9 </StackPanel> 10 </DataTemplate> 11 </DataGridTemplateColumn. cellTemplate> 12 </DataGridTemplateColumn> 13 <DataGridTemplateColumn Header = "file description" Width = "200 *" IsReadOnly = "True"> 14 <DataGridTemplateColumn. cellTemplate> 15 <DataTemplate> 16 <StackPanel Orientation = "Horizontal"> 17 <TextBlock Text = "{Binding FileDesc}" Width = "200" Height = "25" verticalignment = "Center "TextAlignment =" Left "/> 18 19 </Stac KPanel> 20 </DataTemplate> 21 </DataGridTemplateColumn. CellTemplate> 22 </DataGridTemplateColumn> 23 <! -- <Maid Header = "file name" Width = "120 *" Binding = "{Binding FileName}"/> 24 <maid Header = "file description" Width = "150 *" Binding = "{Binding FileDesc}"/> --> 25 </DataGrid. columns> 26 </DataGrid>
To display several columns, add several template columns. The template column is used here. The advantages of the template column are that you can customize columns. In DataTemplete, you can use flexible layout, customize the width and height of the data to be bound and adjust the style of the data to be bound. Note that the AutoGenerateColumns attribute in the DataGrid must be set to "false ",
In the. cs file, assign the set value to the itemsource attribute of the dataGrid, and then bind the DataGrid data .. The code in the cs file is as follows:
1 private void BindData () 2 {3 total = sfList. count; 4 var temp = sfList. skip (pager. pageSize * (pager. pageCurrent-1 )). take (pager. pageSize ). toList (); 5 FileDataGrid. itemsSource = temp; 6} 7 8 // <summary> 9 // load data 10 /// </summary> 11 private void ReLoadData () 12 {13 FileService fs = new FileService (); 14 var tempList = fs. getAllSalaryFile (); 15 sfList = new List <SalaryFile> (); 16 if (tempList! = Null) 17 {18 SalaryFile sf = null; 19 foreach (var item in tempList) 20 {21 sf = new SalaryFile {FileName = item}; 22 sfList. add (sf); 23} 24} 25}