WPF學習1 DataGrid 采用模板列進行數據綁定


先看效果圖,上圖是綁定后的樣子,

窗體界面代碼:

 1                 <DataGrid Name="FileDataGrid" AutoGenerateColumns="False"   Height="150" CanUserAddRows = "false">
 2                     <DataGrid.Columns>
 3                         <DataGridTemplateColumn Header="文件名"  Width="200*" IsReadOnly="True">
 4                             <DataGridTemplateColumn.CellTemplate>
 5                                 <DataTemplate>
 6                                     <StackPanel Orientation="Horizontal">
 7                                         <TextBlock Text="{Binding FileName}" Width="200" Height="25" VerticalAlignment="Center" TextAlignment="Left" />
 8 
 9                                     </StackPanel>
10                                 </DataTemplate>
11                             </DataGridTemplateColumn.CellTemplate>
12                         </DataGridTemplateColumn>
13                         <DataGridTemplateColumn Header="文件描述"  Width="200*" IsReadOnly="True">
14                             <DataGridTemplateColumn.CellTemplate>
15                                 <DataTemplate>
16                                     <StackPanel Orientation="Horizontal">
17                                         <TextBlock Text="{Binding FileDesc}" Width="200" Height="25" VerticalAlignment="Center" TextAlignment="Left" />
18 
19                                     </StackPanel>
20                                 </DataTemplate>
21                             </DataGridTemplateColumn.CellTemplate>
22                         </DataGridTemplateColumn>
23                         <!--<DataGridTextColumn Header="文件名" Width="120*" Binding="{Binding FileName}"/>
24                     <DataGridTextColumn Header="文件描述" Width="150*" Binding="{Binding FileDesc}"/>-->
25                     </DataGrid.Columns>
26                 </DataGrid>

要顯示幾列,就添加幾個模板列,這里采用了模板列,采用模板列的好處,可自定義列,在DataTemplete中,可以采用靈活的排版,自定義要綁定的數據的寬度,高度,調節要綁定數據的樣式,注意,DataGrid 中AutoGenerateColumns屬性一定要設置成"false",
在.cs文件里,把 集合賦值dataGrid的itemsource屬性,這樣就完成DataGrid數據綁定了。.cs文件中的代碼如下:

 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         /// 加載數據
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         }

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM