using LR1.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LR1
{
///
/// Логика взаимодействия для Workers.xaml
///
public partial class Workers : Page
{
public static WorkersEntities DataEntitiesEmployee { get; set; }
public static ObservableCollection ListEmployee;
public Workers()
{
DataEntitiesEmployee = new WorkersEntities();
InitializeComponent();
ListEmployee = new ObservableCollection();
}
private bool isDirty = true;
private bool isLoaded = false;
private void UndoCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isLoaded;
}
private void UndoCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Отмена");
isDirty = true;
isLoaded = false;
}
private void DeleteCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isDirty;
}
private void DeleteCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Удаление");
isDirty = false;
isLoaded = true;
}
private void AddCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isDirty;
}
private void AddCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Employee employee = Employee.CreateEmployee(-1, "не задано", "не задано", "не задано", 0);
employee.Telephone = "не задано";
employee.Email = "не задано";
try
{
DataEntitiesEmployee.Employees.Add(employee);
ListEmployee.Add(employee);
DataGridEmployee.ScrollIntoView(employee);
DataGridEmployee.SelectedIndex = DataGridEmployee.Items.Count - 1;
DataGridEmployee.Focus();
DataGridEmployee.IsReadOnly = false;
MessageBox.Show("Создание");
isDirty = false;
isLoaded = true;
}
catch (DataServiceRequestException ex)
{
throw new ApplicationException("Ошибка добавления нового сотрудника в контекст данных" + ex.ToString());
}
}
private void EditCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isDirty;
}
private void EditCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Редактирование");
DataGridEmployee.IsReadOnly = false;
DataGridEmployee.BeginEdit();
isDirty = false;
isLoaded = true;
}
private void FindCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isDirty;
}
private void FindCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Поиск");
isDirty = false;
isLoaded = true;
}
private void SaveCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isLoaded;
}
private void SaveCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Сохра");
DataEntitiesEmployee.SaveChanges();
isDirty = true;
DataGridEmployee.IsReadOnly=true;
isLoaded = false;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var queryEmployee = DataEntitiesEmployee.Employees.OrderBy(Employee => Employee.Surname);
foreach (Employee emp in queryEmployee)
{
ListEmployee.Add(emp);
}
DataGridEmployee.ItemsSource = ListEmployee;
}
}
}