Answer the question
In order to leave comments, you need to log in
How to remove selected items in DataGrid (WPF)?
Started learning how to work with WPF. I got to the DataGrid component and it’s not very easy to deal with it. The data table itself consists of two columns: Check (DataGridCheckBoxColumn) and Recipe (DataGridTextColumn). It is required to delete the lines that are marked in the Check column. I'm not very familiar with data processing in the DataGrid yet and therefore I want to know how rows are deleted.
<DataGrid x:Name="grid" HorizontalAlignment="Left" Height="298" VerticalAlignment="Top" Width="210" Margin="10,10,0,0" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Check"/>
<DataGridTextColumn Binding="{Binding Path=title}" CanUserSort="False" ClipboardContentBinding="{x:Null}" Header="Recipe" Width="162" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
Answer the question
In order to leave comments, you need to log in
1. Write a viewmodel for each element in the table.
2. Bind the CheckBox column to the IsSelected property of the viewmodel.
3. Bind the DataGrid to an ObservableCollection<ViewModelForItem>.
4. When you need to remove elements, iterate through the viewmodels in the collection and remove those with IsSelected == true.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question