Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I am writing a todolist application in WPF with MVVM. I display the goals in a datagrid and the editing works fine. Changes made in the grid are saved to the DB when I click save. However, adding a...
#1: Initial revision
Datagrid in MVVM saves edits but not inserts
I am writing a todolist application in WPF with MVVM. I display the goals in a datagrid and the editing works fine. Changes made in the grid are saved to the DB when I click save. However, adding a new record does not persist during a save. Each goal may have subtasks associated with it that are handled in a detail row. Perhaps stranger, edits *and additions* to the subtasks **persist**. When debugging, I found that the additions to the grid make it to the ObsdrvableCollection<Goal>, but they are not in the _context.Goals. Edits on existing records can be seen in both the ObservableCollection and in the _context.Goals. What do I need to change to get the additions into the _context? I am using the Microsoft.Toolkit.Mvvm library. public class GoalsViewModel : ObservableObject ... _goals = new ObservableCollection<Goal>(_repository.GetAllGoals().ToList()); ... public ObservableCollection<Goal> Goals { get => _goals; set { SetProperty(ref _goals, value); } } ... public void SaveAll() { _repository.SaveChanges(); } The repository SaveChanges() is merely _context.SaveChanges(); This is the view. <Window x:Class="TodoList.View.GoalsWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TodoList.View" mc:Ignorable="d" Title="Goals" Height="450" Width="800"> <Window.Resources> <CollectionViewSource x:Key="goalsViewSource" Source="{Binding Goals}" /> <CollectionViewSource x:Key="difficultyLevels" Source="{Binding DifficultyLevels}"/> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <DataGrid Grid.Column="0" x:Name="goalsDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" IsReadOnly="False" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding Source={StaticResource goalsViewSource}}" Margin="0,0,0,0" RowDetailsVisibilityMode="VisibleWhenSelected"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding ID}" Header="Id" Width="SizeToCells" ElementStyle="{StaticResource NumericCell}" IsReadOnly="True" /> <DataGridTemplateColumn Width="Auto"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Done!" Command="{Binding DataContext.MarkDoneCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}" Margin="5" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="75" IsReadOnly="False" /> <DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="150" ElementStyle="{StaticResource WrappingText}" IsReadOnly="False" /> <DataGridComboBoxColumn DisplayMemberPath="Name" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource difficultyLevels}}" SelectedValueBinding="{Binding Path=DifficultyLevelID}" Header="Difficulty" Width="SizeToCells" /> <DataGridTextColumn Binding="{Binding GoalItems.Count}" Header="#Items" Width="SizeToHeader" ElementStyle="{StaticResource NumericCell}" IsReadOnly="True" /> <DataGridTextColumn Binding="{Binding DateCreated, StringFormat='MM-dd-yyyy'}" Header="Date Created" Width="SizeToHeader" IsReadOnly="True" /> <DataGridTextColumn Binding="{Binding DateCompleted, StringFormat='MM-dd-yyyy'}" Header="Date Completed" Width="SizeToHeader" IsReadOnly="True" /> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <DataGrid ItemsSource="{Binding GoalItems}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridCheckBoxColumn Binding="{Binding IsCompleted}"/> <DataGridTextColumn Binding="{Binding Description}"/> </DataGrid.Columns> </DataGrid> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid> <Button Grid.Row="1" Grid.Column="0" Command="{Binding SaveAllCommand}" Content="Save All Goals" /> </Grid> </Window>