将 wpf listview 绑定到数据集......可能......? [英] Binding a wpf listview to a Dataset....Possible..?

查看:27
本文介绍了将 wpf listview 绑定到数据集......可能......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力迁移到 Wpf,我只是在尝试将数据绑定到 lsitview 时卡住了.我想将列表视图数据绑定到数据集(数据集,因为我想在列中显示的数据属于不同的表).我是附上我正在尝试的示例代码.它工作正常,但列表只显示一行.可能是错的.任何人都可以指导我完成.所有可用的示例都使用数据表.没有指定绑定到数据集.请帮助..任何意见都将受到高度赞赏...在此先感谢

I was struggling moving to Wpf,I am just stuck while trying out databinding to a lsitview.I want to databind a listview to a dataset(dataset because the data i want to display in columns belongs to different tables).I am attaching a sample code that i am trying with.It works alright but the listliew only shows one row.What could be wrong.Can anyone guide me through.All the samples available are using datatables.None specifies about binding to a dataset.Pls help..any input will be highly appreciated...thanks in advance

我的 Xaml

<Grid>
<TextBox Text="" Height="20" Width="100" HorizontalAlignment="Left" Margin="15,13,0,0" VerticalAlignment="Top"></TextBox>
<TextBox Text="" Height="20" Width="100" HorizontalAlignment="Left" Margin="15,42,0,0" VerticalAlignment="Top"></TextBox>
<ListView Margin="15,89,63,73" Name="lst" ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Path=T1/Name}"></GridViewColumn>
            <GridViewColumn Header="Place" DisplayMemberBinding="{Binding Path=T2/Name}"></GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
<!--<Button Height="19" HorizontalAlignment="Right" Name="button2" VerticalAlignment="Top" Width="46" Margin="0,42,63,0" Click="button2_Click">Add</Button>-->
<Button Height="19" HorizontalAlignment="Right" Name="button1" VerticalAlignment="Top" Width="46" Click="button1_Click" Margin="0,43,63,0">Add</Button>

我的代码

 Dt1 = new DataTable("T1");
        Dt1.Columns.Add("Name");
        Dt1.Rows.Add("abc1");
        Dt1.Rows.Add("abc2");
        Dt2 = new DataTable("T2");
        Dt2.Columns.Add("Name");
        Dt2.Rows.Add("xyz1");
        Dt2.Rows.Add("xyz1");
        Ds = new DataSet();
        Ds.Tables.Add(Dt1);
        Ds.Tables.Add(Dt2);

        lst.DataContext = Ds;

推荐答案

您好,我完全同意 Andy 和 Thomas.他们都优雅地解释了这个概念.

Hi am in full accord with Andy and Thomas. They both have explained the concept elegantly.

我只展示了仅对 dataset 执行相同操作的步骤.

I am only showing the steps of doing the same only with dataset.

MVVM(ModelView ViewModel)我这里不讨论了.

The MVVM (ModelView ViewModel) I am not discussing here.

Xaml 看起来像这样

The Xaml looks like this

<Grid Name="myGrid" ShowGridLines="False">


    <Label Height="28" Margin="12,5,0,0" Name="lblName" VerticalAlignment="Top" HorizontalAlignment="Left" Width="55">Name</Label>
    <TextBox Height="23" Margin="73,8,85,0" Name="txtName" VerticalAlignment="Top" />
    <Label Height="28" Margin="12,39,0,0" Name="lblPlace" VerticalAlignment="Top" HorizontalAlignment="Left" Width="55">Place</Label>
    <TextBox Height="23" Margin="73,44,85,0" Name="txtPlace" VerticalAlignment="Top" />
    <Button Height="23" HorizontalAlignment="Left" Margin="20,82,0,0" Name="btnAddRecord" VerticalAlignment="Top" Width="75" Click="btnAddRecord_Click">Add Record</Button>
    <ListView Margin="31,119,27,45" Name="listView" *ItemsSource="{Binding}"*>

        <ListView.View>

            <GridView>

                <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Name}"/>

                <GridViewColumn Header="Place" DisplayMemberBinding="{Binding Place}"/>


            </GridView>
        </ListView.View>
    </ListView>
</Grid>

在 .CS 文件中创建一个数据集

In the .CS file create a dataset

private DataSet MyDataSet()
    {
        DataTable dtInformation1 = new DataTable();
        dtInformation1.Columns.Add("Name");
        dtInformation1.Columns.Add("Place");
        dtInformation1.Rows.Add(txtName.Text, txtPlace.Text);


        DataTable dtInformation2 = new DataTable();
        dtInformation2.Columns.Add("Name");
        dtInformation2.Columns.Add("Place");
        dtInformation2.Rows.Add(txtName.Text + "2", txtPlace.Text + "2");

        DataSet Ds = new DataSet();
        Ds.Tables.Add(dtInformation1);
        Ds.Tables.Add(dtInformation2);
        return Ds;
    }

接下来在Button的点击事件中写下如下

Next in the Button's click event write the following

private void btnAddRecord_Click(object sender, RoutedEventArgs e)

private void btnAddRecord_Click(object sender, RoutedEventArgs e)

    {

        **listView.ItemsSource = MyDataSet().Tables[0].DefaultView;
              - OR - 
         listView.ItemsSource = MyDataSet().Tables[1].DefaultView;**
    }

注意~您不能将 ListView 的源分配给数据集.

N.B.~ You cannot assign the source of the ListView a dataset.

为什么?你可能会问?数据集,简单来说,就是数据表的集合.

Why ? You may ask? A dataset, in simple terms , is a collection of data tables.

假设您有 5 个不同的数据表.并说它们的列名和列号都不相同.

Suppose you have 5 different datatables. And say none of their column names as well as column numbers are same.

现在您已将所有这些分配给您的数据集.控件源如何知道它必须绑定哪个源?

Now you have assigned all those to your dataset. How will the controls source know that which source it has to bind?

为了克服这种情况,要么制作一个自定义数据表,其中包含这些离散数据表的所有列,并将值分配给这个自定义数据表,然后绑定到源.

Inorder to overcome such a situation, either make a custom datatable that will have all the columns of those discreet datatables and assign the values to this custom one and then bind to the source.

或者你需要在数据源中显式指定数据表

Or you need to explicitly specify the datatable in the datasource

但我总是更喜欢使用 MVVM 模式进行此类操作.

But I always prefer to use MVVM pattern for this kind of operations.

这篇关于将 wpf listview 绑定到数据集......可能......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆