WPF中为ListView动态绑定数据(可参考)

时间:2021-05-31 19:37:39

GridView gv = new GridView();

DataTable dt = fieldManageBLL.GetFieldManage(moduleName);
for ( int  i=0;i<dt.Rows.Count;i++)
{
     if (i==0)
     {
         GridViewColumn gvc= new  GridViewColumn();
         CheckBox cb =  new  CheckBox();
         cb.Checked +=  new  RoutedEventHandler( new  SupplierPayBill().cb_Checked);
         cb.Content =  new  TextBlock().Text =  "全选" ;
         _gvc.Header = cb;
         _gvc.Width = 100;
         _gvc.CellTemplate = (DataTemplate)resources[ "TheCheckBox" ];
         gv.Columns.Add(_gvc);
         continue ;
     }
     gvc.DisplayMemberBinding =  new  Binding(dt.Rows[i][ "FieldManage_Field" ].ToString());
}
return  gv;
 
//其中:dt.Rows[i]["FieldManage_Field"].ToString();分别对应以下DataTemplate中的x:Key的值。
     //至于DisplayMemberBinding我才接触几天,目前的理解是只显示数据而没有其它效果;而CellTemplate则可以在显示的数据上加上控件的效果,比如CheckBox、Hyperlink等
 
//XAML代码:
     <Window.Resources>
         <!-- 复选框 -->
         <DataTemplate x:Key= "TheCheckBox" >
             <TextBlock VerticalAlignment= "Center" >
             <CheckBox Name= "ckb_SelectAll"  Click= "ckb_SelectAll_Click"  Margin= "6 0 0 0" >
                 <!--<TextBlock  Foreground= "White" >全选</TextBlock>-->
             </CheckBox>
             </TextBlock>
         </DataTemplate>
         <!-- 序号 -->
         <DataTemplate x:Key= "TheIndex" >
             <TextBlock Width= "100"  Text= "{Binding Path=TheIndex}"  Style= "{StaticResource NormalTextBlockStyle}"  TextAlignment= "Center" ></TextBlock>
         </DataTemplate>
         <!--单据号-->
         <DataTemplate x:Key= "StoBill_No" >
             <TextBlock Width= "150"  Text= "{Binding Path=StoBill_No}"  Tag= "{Binding Path=StoBill_ID}"  Style= "{StaticResource NormalTextBlockStyle}"  TextAlignment= "Right"  Padding= "0,0,20,0" ></TextBlock>
         </DataTemplate>
         <!--单据类型-->
         <DataTemplate x:Key= "StoBillsType_Name" >
             <TextBlock Width= "100"  Text= "{Binding Path=StoBillsType_Name }"  Style= "{StaticResource NormalTextBlockStyle}"  TextAlignment= "Center" ></TextBlock>
         </DataTemplate>
         <!--时间-->
         <DataTemplate x:Key= "StoBill_OperationDate" >
             <TextBlock Width= "150"  Text= "{Binding Path=StoBill_OperationDate,StringFormat='{}{0:yyyy-MM-dd HH:mm}'}"  Style= "{StaticResource NormalTextBlockStyle}"  TextAlignment= "Left" ></TextBlock>
         </DataTemplate>
         <!--应付-->
         <DataTemplate x:Key= "StoBill_NotPaid" >
             <TextBlock Width= "100"  Text= "{Binding Path=StoBill_NotPaid,StringFormat='N'}"  Style= "{StaticResource NormalTextBlockStyle}"  TextAlignment= "Right"  Padding= "0,0,20,0" ></TextBlock>
         </DataTemplate>
         <!--单据明细-->
         <DataTemplate x:Key= "DanjuDetail" >
             <TextBlock  TextAlignment= "Center"  Width= "100"  >
                <Hyperlink Name= "hpl_DanjuDetailListView"  Tag= "{Binding StoBill_ID}"  Click= "hpl_DanjuDetailListView_Click" >
                 <TextBlock x:Name= "tbl_DanjuDetail"  Text= "单据明细"  />
                </Hyperlink>
             </TextBlock>
         </DataTemplate>
         <!--结算明细-->
         <DataTemplate x:Key= "JieSuanDetail" >
             <TextBlock  TextAlignment= "Center"  Width= "100"  >
                <Hyperlink Name= "hpl_JieSuanDetailListView"  Tag= "{Binding StoBill_ID}"  Click= "hpl_JieSuanDetailListView_Click" >
                 <TextBlock x:Name= "tbl_DanjuDetail"  Text= "结算明细"  />
                </Hyperlink>
             </TextBlock>
         </DataTemplate>
       </Window.Resources>
 
原文:http://www.cnblogs.com/sjrhero/articles/2177994.html