我是靠谱客的博主 谦让凉面,最近开发中收集的这篇文章主要介绍WPF中为ListView动态绑定数据(可参考),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

转载于:https://www.cnblogs.com/lsgsanxiao/p/5920589.html

最后

以上就是谦让凉面为你收集整理的WPF中为ListView动态绑定数据(可参考)的全部内容,希望文章能够帮你解决WPF中为ListView动态绑定数据(可参考)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部