简体   繁体   English

使用复选框列的Custom DataGrid选择要输入表的行

[英]Custom DataGrid using checkbox column to select rows to enter into table

I have built a Silverlight app using ria and MVVM. 我已经使用ria和MVVM构建了Silverlight应用程序。 I have created a ChildWindow that hosts a datagrid. 我创建了一个承载数据网格的ChildWindow。 This datagrid currently loads my <Book> table. 该数据网格当前加载我的<Book>表。 I customized the data grid to bring back just the Book name and I added a checkbox column to select a particular book. 我自定义了数据网格以仅带回书名,并添加了一个复选框列以选择特定的书。 This only pops up when a user wants to multi-select books. 仅在用户要多选书籍时弹出。

My goal is to have users select all the books they want, then hit the save button, add those book names and ids to another table that has a relationship with my book table. 我的目标是让用户选择他们想要的所有书,然后单击“保存”按钮,将那些书名和ID添加到与我的书表有关系的另一个表中。 The book table holds the list of books and book ids, and the second table <JM> is supposed to hold its own id and all the books the user selected. book表包含书籍和书籍ID的列表,第二张表<JM>应该具有其自己的ID和用户选择的所有书籍。 So that later, when a user wants to search for their books, they can call for the book or the multiple book selection and it will return all the books they selected. 这样一来,当用户想要搜索其书籍时,他们可以调用该书籍或选择多个书籍,它将返回他们选择的所有书籍。 Easy stuff. 简单的东西。

However, I am currently unable to add multiple books to the table. 但是,我目前无法将多本书添加到表格中。 Only one book is entered because of my selecteditems code. 由于我选择的项目代码,只输入了一本书。

JobMarket jm = new JobMarket();
foreach (Book b in dataGrid1.SelectedItems)
{
    dataGrid1.SelectedItems.Add(b);
    jm.BookID = b.BookID;
    jm.Book = b.Book1;
}
_context.JobMarkets.Add(jm);
SubmitOperation s = _context.SubmitChanges();

this.DialogResult = true;

I tried it this way because I thought selectedItems would return all selectedItems, but it is currently just the last selected item. 我之所以尝试这种方法,是因为我认为selectedItems将返回所有selectedItems,但是它目前只是最后一个选中的项目。 Since my checkbox control is nested in the datagrid, I am unable to access it via codebehind. 由于我的复选框控件嵌套在datagrid中,因此无法通过代码隐藏访问它。 I know there is a way, I just don't know how to do it. 我知道有办法,我只是不知道该怎么做。 I thought the foreach would loop through and find the checked boxes but it does not. 我以为foreach会循环通过并找到复选框,但事实并非如此。

So I'm looking for a way to fix this and to do all the operations in this one click event. 因此,我正在寻找一种方法来解决此问题并在此一键式事件中执行所有操作。 Is there an easy way to do this in code behind using code similar to my own (my understanding level) Here is the xaml datagrid as well. 是否有一种简单的方法可以在代码后面使用类似于我自己的代码(我的理解水平)来完成此操作,这也是xaml datagrid。

<sdk:DataGrid AutoGenerateColumns="False" Height="532" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" >
    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn Header="Add Book">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="chkAddBook" IsChecked="{Binding Book1, Mode=TwoWay}" />
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn Header="Book">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Book1}" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="12,3"  />
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

If I understand you correctly you want to get all the books that were checked by the user. 如果我对您的理解正确,则希望获取用户检查过的所有书籍。

You can add bool property IsChecked to the Book class and create two way binding to it in Datagridcheckboxcolumn. 您可以将bool属性IsChecked添加到Book类,并在Datagridcheckboxcolumn中创建对其的两种方式绑定。 So later you can select checked books. 因此,以后您可以选择已检查的书籍。

If you don't whant to extend Book class with this property you can create a wrapper for the book with needed property, create collection of these wrappers and bind it to DataGrid. 如果您不希望使用此属性扩展Book类,则可以使用所需的属性为书籍创建包装器,创建这些包装器的集合并将其绑定到DataGrid。

<DataGrid ItemsSource="{Binding MyBooksCollection}">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="{Binding Path=IsChecked, Mode=TwoWay}" />
    </DataGrid.Columns>
</DataGrid>

MyBooks collection should contain the objects with the property IsChecked. MyBooks集合应包含具有IsChecked属性的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM