Everest
Everest

Reputation: 580

Checkbox binding in GridView using silverlight MVVM

There is a GridView which has multiple rows , what i want is, User can select multiple checkboxes and at the end he will press a button which will store all the selected rows to the database. I am Using Silverlight MVVM model for this and my checkbox column looks like this

<c1:DataGridTemplateColumn Header="Select">
    <c1:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel>
                <CheckBox IsEnabled="True" DataContext="{Binding Source={StaticResource view}}"></CheckBox>
            </StackPanel>
        </DataTemplate>
    </c1:DataGridTemplateColumn.CellTemplate>
</c1:DataGridTemplateColumn>

Upvotes: 0

Views: 1908

Answers (1)

AbdouMoumen
AbdouMoumen

Reputation: 3854

There's a control called DataGridCheckBoxColumn that you can use directly:

<sdk:DataGrid ItemsSource="{Binding Items}">
    <sdk:DataGrid.Columns>
        <sdk:DataGridCheckBoxColumn Header="Select"
                                    Binding="{Binding IsChecked, Mode=TwoWay}"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

Then, on the button command, you can use a Linq query to selected the checked elements like so:

var selected = from i in Items
               where i.IsChecked
               select i;

Then, you can save the selected items in the database.

Hope this helps :)

Upvotes: 1

Related Questions