@ -19,42 +19,97 @@ namespace eCompanies
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public delegate void UpdateCompanyGridDelegate ( ) ;
public delegate void UpdateCompanyGridDelegate ( Company updatingCompany ) ;
public partial class MainWindow : Window
{
List < int > deletingCompanyList = new List < int > ( ) ;
List < int > updatingCompanyList = new List < int > ( ) ;
UsersWindow usersWindowInstance = null ;
public MainWindow ( )
{
InitializeComponent ( ) ;
updateCompanyGrid ( ) ;
companyGrid . DataContext = App . getCompanies ( ) ;
}
private void updateCompanyGrid ( )
private void updateCompanyItem ( Company updatingCompany )
{
companyGrid . DataContext = App . getCompanies ( ) ;
if ( updatingCompany = = null ) return ;
updatingCompanyList . Add ( updatingCompany . CompanyId ) ;
var context = ( ( ObservableCollection < Company > ) companyGrid . DataContext ) . ToList < Company > ( ) ;
for ( int i = 0 ; i < context . Count ; i + + )
{
if ( context [ i ] . CompanyId . Equals ( updatingCompany . CompanyId ) )
{
context [ i ] = updatingCompany ;
companyGrid . DataContext = new ObservableCollection < Company > ( context ) ;
updatingCompanyList . Add ( updatingCompany . CompanyId ) ;
return ;
}
}
}
private void RowEdit_Click ( object sender , RoutedEventArgs e )
{
try
UpdateCompanyGridDelegate d = updateCompanyItem ;
Company company = ( Company ) companyGrid . CurrentItem ;
if ( usersWindowInstance ! = null ) usersWindowInstance . Close ( ) ;
usersWindowInstance = new UsersWindow ( company , d ) ;
usersWindowInstance . Show ( ) ;
}
private void RowDelete_Click ( object sender , RoutedEventArgs e )
{
deleteCompanyFromGrid ( ) ;
}
private void deleteCompanyFromGrid ( )
{
var companies = ( ObservableCollection < Company > ) companyGrid . DataContext ;
foreach ( Company c in companies )
{
UpdateCompanyGridDelegate d = updateCompanyGrid ;
Company company = ( Company ) companyGrid . CurrentItem ;
if ( usersWindowInstance ! = null ) usersWindowInstance . Close ( ) ;
usersWindowInstance = new UsersWindow ( company , d ) ;
usersWindowInstance . Show ( ) ;
int deletingUser = ( ( Company ) companyGrid . CurrentItem ) . CompanyId ;
if ( Equals ( c . CompanyId , deletingUser ) )
{
deletingCompanyList . Add ( ( ( Company ) companyGrid . CurrentItem ) . CompanyId ) ;
companies . Remove ( c ) ;
companyGrid . DataContext = companies ;
return ;
}
}
catch { }
}
private void RowDelete_Click ( object sender , RoutedEventArgs e )
private void Window_Closing ( object sender , System . ComponentModel . Cancel EventArgs e )
{
try
MessageBoxResult mbr = MessageBox . Show ( "Вы закрываете приложение. Хотите сохранить результаты?" , "Сохранение изменений" , MessageBoxButton . YesNo ) ;
if ( mbr = = MessageBoxResult . No )
return ;
foreach ( Company c in ( ObservableCollection < Company > ) companyGrid . DataContext )
{
Company company = ( Company ) companyGrid . CurrentItem ;
App . removeCompany ( company ) ;
if ( c . Name = = null )
{
mbr = MessageBox . Show ( "Вы не заполнили поле [Название компании]! " +
"Вернуться к форме для исправления? (При ответе Нет все данные будут потеряны!)" ,
"Поле [Название компании] не может быть пустым!" ,
MessageBoxButton . YesNo ) ;
if ( mbr = = MessageBoxResult . Yes )
{
e . Cancel = true ;
return ;
}
else if ( mbr = = MessageBoxResult . No )
return ;
}
if ( deletingCompanyList ! = null )
App . removeCompany ( deletingCompanyList ) ;
deletingCompanyList = null ;
// TODO: реализация обновления старых компаний и добавления новых
}
catch { }
}
}
}