📅  最后修改于: 2023-12-03 15:13:48.693000             🧑  作者: Mango
If you are a C# developer looking to implement CRUD operations with an ObservableCollection in an MVVM architecture, then you are in the right place! In this guide, we will cover all the necessary steps to create a basic MVVM project and implement CRUD operations using an ObservableCollection in C#.
First, let's create a new project in Visual Studio.
Now that we have created a new project, we need to organize our code according to the MVVM architecture.
We will start by creating a simple model class to represent our data.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
Our view model class will implement the logic for our CRUD operations using an ObservableCollection of Person objects.
public class MainViewModel : INotifyPropertyChanged
{
private ObservableCollection<Person> people;
public ObservableCollection<Person> People
{
get { return people; }
set
{
people = value;
RaisePropertyChanged("People");
}
}
public MainViewModel()
{
People = new ObservableCollection<Person>();
}
public void AddPerson(Person person)
{
People.Add(person);
}
public void UpdatePerson(Person person)
{
// Find the person in the collection and update their properties
int index = People.IndexOf(person);
if (index != -1)
{
People[index] = person;
}
}
public void DeletePerson(Person person)
{
People.Remove(person);
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
#endregion
}
Finally, let's create a view to display our data and allow users to perform CRUD operations.
<Window x:Class="MyNamespace.MainWindow"
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:MyNamespace"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Add / Update Form -->
<GroupBox Header="Add / Update Person" Grid.Row="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="First Name:"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding SelectedPerson.FirstName, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="1" Grid.Column="0" Content="Last Name:"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectedPerson.LastName, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="2" Grid.Column="0" Content="Age:"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding SelectedPerson.Age, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="3" Grid.ColumnSpan="2" Content="Save" Command="{Binding SaveCommand}"/>
</Grid>
</GroupBox>
<!-- Data Grid -->
<DataGrid ItemsSource="{Binding People}" Grid.Row="1" SelectedItem="{Binding SelectedPerson}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
</DataGrid.Columns>
</DataGrid>
<!-- Delete Button -->
<Button Grid.Row="1" Content="Delete" Command="{Binding DeleteCommand}"
VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,5,5,0"/>
</Grid>
</Window>
Now, we need to bind our view to our view model class.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Set the DataContext of the view to our view model
DataContext = new MainViewModel();
}
}
Now let's test our CRUD operations.
Congratulations! You have successfully implemented CRUD operations using an ObservableCollection in an MVVM architecture in C#.