一个WPF列表视图绑定到数据集....可能..? [英] Binding a wpf listview to a Dataset....Possible..?

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

问题描述

我挣扎搬到WPF中,我只是停留在试数据绑定到lsitview.I要数据绑定列表视图到数据集(数据集,因为我想在列中显示的数据属于不同的表)。我是附加样品code,我想with.It工作好吗但listliew只显示了一种row.What可能是wrong.Can有人指导我through.All可使用datatables.None样品指定有关绑定到数据集.PLS help..any投入将是非常美联社preciated ...在此先感谢

我的XAML

 <电网>
<文本框文本=高度=20WIDTH =100的Horizo​​ntalAlignment =左保证金=15,13,​​0,0VerticalAlignment =热门>< /文本框>
<文本框文本=高度=20WIDTH =100的Horizo​​ntalAlignment =左保证金=15,42,0,0VerticalAlignment =热门>< /文本框>
< ListView的保证金=15,89,63,73NAME =乐善堂的ItemsSource ={结合}>
    < ListView.View>
        < GridView控件>
            < GridViewColumn标题=名称DisplayMemberBinding ={绑定路径= T1 /名称}>< / GridViewColumn>
            < GridViewColumn标题=广场DisplayMemberBinding ={绑定路径= T2 /名称}>< / GridViewColumn>
        < / GridView的>
    < /ListView.View>
< / ListView控件>
<! - <按钮高度=19的Horizo​​ntalAlignment =右NAME =按钮2VerticalAlignment =热门WIDTH =46保证金=0,42,63,0点击=button2_Click&GT ;添加< /按钮>  - >
<按钮高度=19的Horizo​​ntalAlignment =右NAME =Button1的VerticalAlignment =热门WIDTH =46点击=的button1_Click保证金=0,43,63,0>添加< /按钮&GT ;
 

我的code

  DT1 =新的DataTable(T1);
        Dt1.Columns.Add(姓名);
        Dt1.Rows.Add(ABC1);
        Dt1.Rows.Add(ABC2);
        DT2 =新的DataTable(T2);
        Dt2.Columns.Add(姓名);
        Dt2.Rows.Add(XYZ1);
        Dt2.Rows.Add(XYZ1);
        DS =新的DataSet();
        Ds.Tables.Add(DT1);
        Ds.Tables.Add(DT2);

        lst.DataContext = Ds的;
 

解决方案

是完全符合安迪和托马斯喜。它们都具有典雅的解释这个概念。

我只显示做同样仅是

步骤

在MVVM(模型视图视图模型)我不会在这里讨论的。

XAML中看起来像这样

 <网名=myGridShowGridLines =FALSE>


    <标识高度=28保证金=12,5,0,0NAME =lblNameVerticalAlignment =热门的Horizo​​ntalAlignment =左WIDTH =55>名称< /标签>
    <文本框高度=23保证金=73,8,85,0NAME =改为txtNameVerticalAlignment =热门/>
    <标识高度=28保证金=12,39,0,0NAME =lblPlaceVerticalAlignment =热门的Horizo​​ntalAlignment =左WIDTH =55>地方< /标签>
    <文本框高度=23保证金=73,44,85,0NAME =txtPlaceVerticalAlignment =热门/>
    <按钮高度=23的Horizo​​ntalAlignment =左保证金=20,82,0,0NAME =btnAddRecordVerticalAlignment =热门WIDTH =75点击=btnAddRecord_Click>添加记录< /按钮>
    < ListView的保证金=31,119,27,45NAME =ListView控件*的ItemsSource ={结合}*>

        < ListView.View>

            < GridView控件>

                < GridViewColumn标题=名称DisplayMemberBinding ={结合名}/>

                < GridViewColumn标题=广场DisplayMemberBinding ={结合地方}/>


            < / GridView的>
        < /ListView.View>
    < / ListView控件>
< /网格>
 

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

 私有数据集MyDataSet()
    {
        数据表dtInformation1 =新的DataTable();
        dtInformation1.Columns.Add(姓名);
        dtInformation1.Columns.Add(地方);
        dtInformation1.Rows.Add(txtName.Text,txtPlace.Text);


        数据表dtInformation2 =新的DataTable();
        dtInformation2.Columns.Add(姓名);
        dtInformation2.Columns.Add(地方);
        dtInformation2.Rows.Add(txtName.Text +2,txtPlace.Text +2);

        数据集DS =新的DataSet();
        Ds.Tables.Add(dtInformation1);
        Ds.Tables.Add(dtInformation2);
        返回Ds的;
    }
 

下一步按钮的Click事件中写入以下

私人无效btnAddRecord_Click(对象发件人,RoutedEventArgs E)

  {

        ** listView.ItemsSource = MyDataSet()桌子[0] .DefaultView;
              - 要么 -
         listView.ItemsSource = MyDataSet()桌子[1] .DefaultView。**
    }
 

NB〜你不能指定的ListView源的数据集。

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

假设你有5个不同的数据表。并说没有他们的列名,以及列数字是一样的。

现在您已分配的所有那些数据集。对照源如何知道它绑定了哪些资源?

中序,以克服这种情况,要么做一个自定义的数据表中,将有那些不显眼的数据表的所有列和分配值这个自定义的,然后绑定到源。

或者你需要在数据源明确指定的数据表

但是,我一直preFER使用MVVM模式为这种操作。

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

My 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>

My Code

 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;

解决方案

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

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

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

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>

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;
    }

Next in the Button's click event write the following

private void btnAddRecord_Click(object sender, RoutedEventArgs e)

    {

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

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.

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

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

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

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