Answer the question
In order to leave comments, you need to log in
How to perform CRUD operations using Entity Framework?
I'm trying to figure out a bunch of WPF, MVVM Patern, Entity Framework.
Currently there is:
MainWindow.xaml
<Window x:Class="CashReg.Client.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" Title="Банки">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0"
ItemsSource="{Binding Banks}"
SelectedItem="{Binding SelectedItem}"
AutoGenerateColumns="False"
IsReadOnly="True"
CanUserAddRows="False" CanUserSortColumns="True">
<DataGrid.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding city}" Header="Город"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Name}" Header="Название"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding bik}" Header="БИК"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding kor_acc}" Header="Корр. счет"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="1" Content="Сохранить" Command="{Binding Save}" HorizontalAlignment="Left"></Button>
</Grid>
</Window>
using System;
using System.Windows;
namespace CashReg.Client
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MainWindowViewModel _viewModel = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _viewModel;
}
protected override void OnClosed(EventArgs e)
{
_viewModel.Dispose();
base.OnClosed(e);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.ObjectModel;
using CashReg.Data;
using System.Runtime.CompilerServices;
namespace CashReg.Client
{
public class MainWindowViewModel : INotifyPropertyChanged, IDisposable
{
/// <summary>
/// Наш контекст данных
/// </summary>
private CashRegContext _dataContext;
public ObservableCollection<Bank> Banks { get; private set; }
public MainWindowViewModel()
{
_dataContext = new CashRegContext();
Banks = new ObservableCollection<Bank>(_dataContext.Banks);
Save = new ActionCommand(SaveChanges) { IsExecutable = true };
}
private void SaveChanges()
{
_dataContext.SaveChanges();
}
public ActionCommand Save { get; set; }
public ActionCommand DeleteCommand { get; set; }
private Bank selectedItem;
public Bank SelectedItem
{
get { return selectedItem; }
set
{
if (selectedItem == value)
return;
selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public void Dispose()
{
// освобождение ресурсов контекста при удалении ViewModel
_dataContext.Dispose();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace CashReg.Client
{
public class ActionCommand : ICommand
{
private Action _action;
private bool _isExecutable;
public bool IsExecutable
{
get { return _isExecutable; }
set
{
_isExecutable = value;
if (CanExecuteChanged == null)
{
return;
}
CanExecuteChanged(this, new EventArgs());
}
}
public ActionCommand(Action action)
{
_action = action;
}
/// <summary>
/// Предикат показывает можно ли запускать команды при заданном аргументе.
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return IsExecutable;
}
public event EventHandler CanExecuteChanged;
/// <summary>
/// Что должна выполнять команда
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
_action();
}
}
}
Answer the question
In order to leave comments, you need to log in
_dataContext.Banks.Add(new Bank());
_dataContext.Banks.Remove(bank));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question