WPF Learning 1 DataGrid uses template columns for Data Binding

Source: Internet
Author: User

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}

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.