简体   繁体   中英

Custom DataGrid using checkbox column to select rows to enter into table

I have built a Silverlight app using ria and MVVM. I have created a ChildWindow that hosts a datagrid. This datagrid currently loads my <Book> table. 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. 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. 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. Since my checkbox control is nested in the datagrid, I am unable to access it via codebehind. 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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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