首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在XamGrid中显示多行

如何在XamGrid中显示多行
EN

Stack Overflow用户
提问于 2013-07-17 20:29:21
回答 1查看 253关注 0票数 0

我正在使用ListBox控件来显示项目,当我双击Cilck时,项目意味着特定的项目字段将在xamgrid中显示,问题是当我选择项目意味着它的显示,但我需要在网格中显示一个或多个项目的多个内容

我的代码是,

代码语言:javascript
复制
private void LsImageGallery_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            RTBSPROJECT.DB.RTBSInventoryMgmtEntities2 MyRTBSinventoryDB = new DB.RTBSInventoryMgmtEntities2();

            RTBSPROJECT.DB.RTBSInventoryMgmtEntities2 DB_Linq = new DB.RTBSInventoryMgmtEntities2();
         try
        {

        string curItem = LsImageGallery.SelectedItem.ToString();

        #region

        if (cmbItemCombo.SelectedItem == null)
        {


            var SelectedImage = (ImageEntity)this.LsImageGallery.SelectedItem;

            string ItemName = SelectedImage.ItemName_EN;

            var query = (from MyItems in MyRTBSinventoryDB.tbl_ItemMaster
                         where MyItems.ActiveFlag == true &&
                         MyItems.ItemName_EN == ItemName
                         select MyItems).ToList();

            xamGrid1.ItemsSource = query;
}

每次显示一条记录时,如果我选择了第二条,意味着之前选择的两条记录都不应该清除,请帮助我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-17 22:34:30

我不是100%确定你在这里问什么,但我会试一试。在我看来,您正在尝试向DataGrid中添加更多行,但实际上只显示了新的项。

这是因为您正在更改DataGrid的ItemsSource属性,如果您希望将新项目添加到底部,则ItemsSource需要保持不变,而您需要添加新行。

这可以使用DataGrid上的Items.Add()方法来完成,循环您的数据行并为每个数据行调用add,或者您也可以创建一个项目集合,只需双击就可以添加到其中,我在这里提供了第二个项目的示例。

与您的应用程序类似,它显示一个ListBox和一个DataGrid,每次您双击ListBox中的一个项目时,DataGrid中就会多添加几行。

XAML

代码语言:javascript
复制
 <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" ItemsSource="{Binding Items}" MouseDoubleClick="ListBox_MouseDoubleClick"/>
            <DataGrid Grid.Column="1" ItemsSource="{Binding GridItems}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding}"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

代码隐藏

代码语言:javascript
复制
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        // This collection is bound to the ListBox so there are items to select from.
        public ObservableCollection<object> Items
        {
            get { return (ObservableCollection<object>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }
        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(ObservableCollection<object>), typeof(MainWindow), new PropertyMetadata(null));

        // On double click of each item in the ListBox more items will be added to this collection.
        public ObservableCollection<object> GridItems
        {
            get { return (ObservableCollection<object>)GetValue(GridItemsProperty); }
            set { SetValue(GridItemsProperty, value); }
        }       
        public static readonly DependencyProperty GridItemsProperty =
            DependencyProperty.Register("GridItems", typeof(ObservableCollection<object>), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            InitializeComponent();

            // Some demo items so we can double click.
            Items = new ObservableCollection<object>();
            Items.Add("test item 1");
            Items.Add("test item 2");
            Items.Add("test item 3");
            Items.Add("test item 4");
            Items.Add("test item 5");
            Items.Add("test item 6");
            Items.Add("test item 7");

            GridItems = new ObservableCollection<object>();
        }

        private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // These would be the entries from your database, I'm going to add random numbers.
            Random rnd = new Random();

            for (int i = 0; i < rnd.Next(5); i++)
                GridItems.Add(rnd.Next(100));
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17699798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档