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

Posted 三小

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于WPF中为ListView动态绑定数据(可参考)的主要内容,如果未能解决你的问题,请参考以下文章

C#WPF,ListView和GridView,动态列以及数据绑定问题

请问 WPF中如何动态改变 ListView中 某一个值 颜色。

wpf中怎么为treeview动态添加子菜单

可观察集合的 C# wpf listview 绑定问题

ComboBox在WPF中的绑定示例:绑定项集合转换,及其源代码

WPF 如何实现可编辑表格,类似Excel