Dynamically binding data for a ListView in WPF (refer to)

Source: Internet
Author: User

GridView gv = newGridView();

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>Original: http://www.cnblogs.com/sjrhero/articles/2177994.html

Dynamically binding data for a ListView in WPF (Reference)

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.