I have a class Employee :Person
Class Employee
[Serializable]
public enum Education
{
secondary, specialized_secondary, high
}
[Serializable]
public enum MarriageStatus
{
single, married, divorced
}
[Serializable]
public class Employee : Person, INotifyPropertyChanged
{
private Education _teaching;
private MarriageStatus _status;
private string _photoPath;
public Education Teaching
{
get { return _teaching; }
set
{
_teaching = value;
OnPropertyChanged("Teaching");
}
}
public MarriageStatus Status
{
get { return _status; }
set
{
_status = value;
OnPropertyChanged("Status");
}
}
public string PhotoPath
{
get { return _photoPath; }
set
{
_photoPath = value;
OnPropertyChanged("Status");
}
}
private ObservableCollection<Employee> _employeesList = null;
public new event PropertyChangedEventHandler PropertyChanged;
public new void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
public ObservableCollection<Employee> EmployeesList
{
get
{
if (_employeesList != null)
{
return _employeesList;
}
_employeesList = new ObservableCollection<Employee>();
_employeesList.Add(new Employee()
{
Id = 1,
FirstName = "Igor",
LastName = "Krivonos",
DateBirthday = new DateTime(1999, 8, 15),
INN = "111111111",
Teaching = Education.high,
Status = MarriageStatus.married,
PhotoPath = "Photo/IgorKrivonos.jpg"
});
return _employeesList;
}
}
}
I have realized INotifyPropety event but I am not sure about this. I am new in this field and trying everything that might help. All my Employee informaiton is shown in DataGrid When I change any cell there it will not show in debuger any changes. I want to serialize it to save changes. Why my changing is not working? Sorry for my English.
Here is my xaml code:
Window x:Class="Employee_DataGrid.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:Employee_DataGrid"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:model="clr-namespace:Employee_DataGrid.Model"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<model:Employee x:Key="employees"></model:Employee>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="status">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="model:MarriageStatus" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="education">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="model:Education" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--Button for serialization-->
<Button Grid.Column="1" Click="Button_Click" >
<TextBlock TextAlignment="Center" FontSize="35" FontFamily="TimesNewRoman" FontWeight="Bold" Width="30" TextWrapping="Wrap">Save Data</TextBlock>
</Button>
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False"
ItemsSource="{Binding Source={StaticResource employees}, Path=EmployeesList}">
<!--DataGrid Columns-->
<DataGrid.Columns>
<!--DataGridTextColumn for Full names and Inn-->
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></DataGridTextColumn>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></DataGridTextColumn>
<DataGridTextColumn Header="INN" Binding="{Binding INN}"></DataGridTextColumn>
<!--DataGridComboBoxColumn for Marriage Status-->
<DataGridComboBoxColumn Header="Status"
ItemsSource="{Binding Source={StaticResource status}}"
SelectedValueBinding="{Binding Status}" >
</DataGridComboBoxColumn>
<!--DataGridTemplateColumn for Birthday-->
<DataGridTemplateColumn Header="Date Birthday">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding DateBirthday, StringFormat='MM.dd.yyyy'}"></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--DataGridComboBoxColumn for Education-->
<DataGridComboBoxColumn Header="Education"
ItemsSource="{Binding Source={StaticResource education}}"
SelectedValueBinding="{Binding Teaching}" >
</DataGridComboBoxColumn>
<!--DataGridTemplateColumn for Photos-->
<DataGridTemplateColumn Header="Employees Photos">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image DockPanel.Dock="Right"
HorizontalAlignment="Right"
Width="70"
Source="{Binding Path=PhotoPath}"></Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
I inherit some propeties from Person Class Here is my Person Class:
[Serializable]
public class Person: INotifyPropertyChanged
{
private int _id;
private string _firstName;
private string _lastName;
private System.DateTime _dateBirthday;
private string _inn;
public int Id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged("Id");
}
}
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
OnPropertyChanged("FirstName");
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
OnPropertyChanged("LastName");
}
}
public System.DateTime DateBirthday
{
get { return _dateBirthday; }
set
{
_dateBirthday = value;
OnPropertyChanged("DateBirthday");
}
}
public string INN
{
get { return _inn; }
set
{
_inn = value;
OnPropertyChanged("INN");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
Here I have realized INotifyPropety as well.
Main Window class
[Serializable]
public partial class MainWindow : Window
{
public Employee Employee { get; set; }
public MainWindow()
{
InitializeComponent();
Employee = new Employee();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Helpers.Serializing(Employee.EmployeesList, "employees.bin");
}
}
Go to Source
Author: Alex