Browse Source

Статусы всех DataGridRow теперь обновляются при соответствующих событиях. Также восстановлена работа метода, осуществлявшего перенос данных из формы компании в форму всех компаний.

master
Никита 6 years ago
parent
commit
fecb1adae0
  1. 3
      eCompanies/MainWindow.xaml
  2. 22
      eCompanies/MainWindow.xaml.cs
  3. 3
      eCompanies/UsersWindow.xaml
  4. 20
      eCompanies/UsersWindow.xaml.cs

3
eCompanies/MainWindow.xaml

@ -8,7 +8,8 @@
Title="MainWindow" Height="450" Width="800" Closing="Window_Closing"> Title="MainWindow" Height="450" Width="800" Closing="Window_Closing">
<Grid> <Grid>
<DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" VerticalAlignment="Top" <DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" VerticalAlignment="Top"
Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="companyGrid_CellEditEnding"> Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="companyGrid_CellEditEnding"
AddingNewItem="companyGrid_AddingNewItem">
<DataGrid.Resources> <DataGrid.Resources>
<local:HiddenConverter x:Key="HiddenConverter"></local:HiddenConverter> <local:HiddenConverter x:Key="HiddenConverter"></local:HiddenConverter>
<ContextMenu x:Key="RowMenu" <ContextMenu x:Key="RowMenu"

22
eCompanies/MainWindow.xaml.cs

@ -24,11 +24,29 @@ namespace eCompanies
private void updateCompanyItem(Company updatingCompany) private void updateCompanyItem(Company updatingCompany)
{ {
if (updatingCompany == null) return; if (updatingCompany == null) return;
var c = ((DataGrid)companyGrid).DataContext as ObservableCollection<Company>;
for (int i = 0; i != c.Count(); i++)
if (c[i].CompanyId.Equals(updatingCompany.CompanyId))
c[i] = updatingCompany;
companyGrid.Items.Refresh();
}
private void companyGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
e.NewItem = new Company();
var newItem = e.NewItem as Company;
newItem.dataGridRowStatus = DataGridRowStatus.NEW;
var c = ((DataGrid)sender).DataContext as ObservableCollection<Company>;
if (c.Count != 0)
newItem.CompanyId = ++c[c.Count - 1].CompanyId;
} }
private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{ {
((Company)e.Row.Item).dataGridRowStatus = DataGridRowStatus.UPDATED; var context = e.Row.DataContext as Company;
if (context.dataGridRowStatus != DataGridRowStatus.NEW)
context.dataGridRowStatus = DataGridRowStatus.UPDATED;
} }
private void RowEdit_Click(object sender, RoutedEventArgs e) private void RowEdit_Click(object sender, RoutedEventArgs e)
@ -36,7 +54,7 @@ namespace eCompanies
UpdateCompanyGridDelegate d = updateCompanyItem; UpdateCompanyGridDelegate d = updateCompanyItem;
Company company = (Company)companyGrid.CurrentItem; Company company = (Company)companyGrid.CurrentItem;
if (usersWindowInstance != null) usersWindowInstance.Close(); if (usersWindowInstance != null) usersWindowInstance.Close();
usersWindowInstance = new UsersWindow(company, d, deletingUsersList); usersWindowInstance = new UsersWindow(company, d);
usersWindowInstance.Show(); usersWindowInstance.Show();
} }

3
eCompanies/UsersWindow.xaml

@ -12,7 +12,8 @@
<ColumnDefinition Width="0*"/> <ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<DataGrid x:Name="usersGrid" HorizontalAlignment="Left" Height="354" Margin="10,55,0,0" VerticalAlignment="Top" <DataGrid x:Name="usersGrid" HorizontalAlignment="Left" Height="354" Margin="10,55,0,0" VerticalAlignment="Top"
Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False"> Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="usersGrid_CellEditEnding"
AddingNewItem="usersGrid_AddingNewItem">
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding UserId}"/> <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding UserId}"/>
<DataGridTextColumn Header="Имя пользователя" Width="*" Binding="{Binding Name}" /> <DataGridTextColumn Header="Имя пользователя" Width="*" Binding="{Binding Name}" />

20
eCompanies/UsersWindow.xaml.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
namespace eCompanies namespace eCompanies
@ -15,7 +16,7 @@ namespace eCompanies
Company currentCompany; Company currentCompany;
UpdateCompanyGridDelegate updateCompanyItem; UpdateCompanyGridDelegate updateCompanyItem;
public UsersWindow(Company c, UpdateCompanyGridDelegate d, List<int> deletingUsersList) public UsersWindow(Company c, UpdateCompanyGridDelegate d)
{ {
InitializeComponent(); InitializeComponent();
currentCompany = c; currentCompany = c;
@ -75,5 +76,22 @@ namespace eCompanies
((Users)usersGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED; ((Users)usersGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED;
usersGrid.Items.Refresh(); usersGrid.Items.Refresh();
} }
private void usersGrid_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
var context = e.Row.DataContext as Users;
if (context.dataGridRowStatus != DataGridRowStatus.NEW)
context.dataGridRowStatus = DataGridRowStatus.UPDATED;
}
private void usersGrid_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e)
{
e.NewItem = new Users();
var newItem = e.NewItem as Users;
newItem.dataGridRowStatus = DataGridRowStatus.NEW;
var c = ((DataGrid)sender).DataContext as ObservableCollection<Users>;
if (c.Count != 0)
newItem.UserId = ++c[c.Count - 1].UserId;
}
} }
} }

Loading…
Cancel
Save