【问题标题】:How to get data from listview/gridview and populate another listview/gridview?如何从 listview/gridview 获取数据并填充另一个 listview/gridview?
【发布时间】:2011-03-26 00:14:56
【问题描述】:

使用带有绑定的 XAML 列表视图/网格视图我使用 linq 查询和 C# 代码填充网格:

                AdventureWorkEntities awDatabase = new AdventureWorkEntities();
                var products = from p in awDatabase.Products
                               from i in awDatabase.ProductInventories
                               where p.ProductID == i.ProductID && p.ListPrice > 0
                               && p.Name.Contains(search.Text.Trim())
                               select new
                               {
                                   p.ListPrice,
                                   p.Name,
                                   p.ProductNumber,
                                   p.DaysToManufacture,
                                   i.Quantity
                               };

                IListSource query = (IListSource)products;
                ProductsList.ItemsSource = query.GetList();

我注意到所选项目的双击事件。能够双击一行并将该行传输到其下方的另一个网格的语法是什么?

关键是我希望能够通过双击一个网格中的选定项目将一行从一个网格添加到另一个网格。

编辑:XAML 代码:

 <ListView Name="ProductsList"  IsSynchronizedWithCurrentItem="True" DataContext="{Binding}" 
                  Margin="6,76,6,220" Width="726" MouseDoubleClick="ProductsList_MouseDoubleClick">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn  Width="85" Header="Product Number"
                                            DisplayMemberBinding="{Binding Path=ProductNumber}"/>
                            <GridViewColumn Width="225" Header="Name"
                                            DisplayMemberBinding="{Binding Path=Name}"/>
                            <GridViewColumn Width="135" Header="Days To Manufacture"
                                                DisplayMemberBinding="{Binding Path=DaysToManufacture}"/>
                            <GridViewColumn Width="75" Header="Quantity"
                                                DisplayMemberBinding="{Binding Path=Quantity}"/>
                            <GridViewColumn Width="75" Header="List Price"
                                                DisplayMemberBinding="{Binding Path=ListPrice}"/>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
        </ListView>

我的尝试:

        private void ProductsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //code to place the contents of top grid to bottome grid

        List<IQueryable> selectedContents = new List<IQueryable>();
        selectedContents.Add((IQueryable)ProductsList.SelectedValue);
        IListSource query = (IListSource)selectedContents;
        OrderContents.ItemsSource= query.GetList();
    }

【问题讨论】:

  • 您需要在此处提供更多数据。你说的是listview还是grid,你在这里绑定了什么样的数据?是桌子吗?列表?或任何自定义对象。
  • 我已经展示了我正在处理的代码。我需要能够从一个网格中选择一行并将其放入另一个网格中。

标签: c# wpf xaml listview gridview


【解决方案1】:

由于缺乏语法知识,我无法弄清楚这一行。

        private void ProductsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //moves items from top grid to bottom grid
        OrderContents.Items.Add(ProductsList.SelectedValue);   
    }

【讨论】:

    【解决方案2】:

    您需要制作实体的 Observable Collection ...并在双击事件中从 listView 中获取所选实体并将所选实体添加到该 Observable Collection 中并将该集合与第二个 listview 绑定...。

    【讨论】:

    • 通过制作实体的可观察集合,您的意思是制作数据类并将其放入集合中吗?
    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 2016-03-02
    相关资源
    最近更新 更多