Skip to content

Commit edba839

Browse files
committed
lesson exercises
1 parent 7732959 commit edba839

7 files changed

+245
-13
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Window x:Class="Lesson01.AddStudentModal"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:local="clr-namespace:Lesson01"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
Title="New Student"
8+
Width="250"
9+
Height="350"
10+
WindowStartupLocation="CenterScreen"
11+
mc:Ignorable="d">
12+
<StackPanel HorizontalAlignment="Center">
13+
<TextBlock Margin="0,20,0,0" Text="Student Id" />
14+
<TextBox x:Name="IdTextBox" Width="150" />
15+
16+
<TextBlock Margin="0,20,0,0" Text="Name" />
17+
<TextBox x:Name="NameTextBox" Width="150" />
18+
19+
<TextBlock Margin="0,20,0,0" Text="Grade" />
20+
<TextBox x:Name="GradeTextBox" Width="150" />
21+
22+
<Button Margin="0,20,0,0"
23+
Background="#32a852"
24+
Click="Save_Click"
25+
Content="Save" />
26+
</StackPanel>
27+
</Window>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Lesson01.Models;
2+
using System.Windows;
3+
4+
namespace Lesson01
5+
{
6+
/// <summary>
7+
/// Interaction logic for AddStudentModal.xaml
8+
/// </summary>
9+
public partial class AddStudentModal : Window
10+
{
11+
private readonly List<Student> students;
12+
13+
public AddStudentModal(List<Student> students)
14+
{
15+
InitializeComponent();
16+
17+
this.students = students;
18+
}
19+
20+
private void Save_Click(object sender, RoutedEventArgs e)
21+
{
22+
int id = int.Parse(IdTextBox.Text);
23+
string name = NameTextBox.Text;
24+
int grade = int.Parse(GradeTextBox.Text);
25+
26+
var newStudent = new Student(id, name, grade);
27+
students.Add(newStudent);
28+
29+
Close();
30+
}
31+
}
32+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Window x:Class="Lesson01.EditStudentModal"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:local="clr-namespace:Lesson01"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
Title="New Student"
8+
Width="250"
9+
Height="350"
10+
WindowStartupLocation="CenterScreen"
11+
mc:Ignorable="d">
12+
<StackPanel HorizontalAlignment="Center">
13+
<TextBlock Margin="0,20,0,0" Text="Student Id" />
14+
<TextBox x:Name="IdTextBox" Width="150" />
15+
16+
<TextBlock Margin="0,20,0,0" Text="Name" />
17+
<TextBox x:Name="NameTextBox" Width="150" />
18+
19+
<TextBlock Margin="0,20,0,0" Text="Grade" />
20+
<TextBox x:Name="GradeTextBox" Width="150" />
21+
22+
<Button Margin="0,20,0,0"
23+
Background="#32a852"
24+
Click="Save_Click"
25+
Content="Save" />
26+
</StackPanel>
27+
</Window>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Lesson01.Models;
2+
using System.Windows;
3+
4+
namespace Lesson01
5+
{
6+
/// <summary>
7+
/// Interaction logic for EditStudentModal.xaml
8+
/// </summary>
9+
public partial class EditStudentModal : Window
10+
{
11+
private readonly Student student;
12+
public EditStudentModal(Student student)
13+
{
14+
InitializeComponent();
15+
16+
this.student = student;
17+
18+
IdTextBox.Text = student.Id.ToString();
19+
NameTextBox.Text = student.Name;
20+
GradeTextBox.Text = student.Grade.ToString();
21+
}
22+
23+
private void Save_Click(object sender, RoutedEventArgs e)
24+
{
25+
student.Id = int.Parse(IdTextBox.Text);
26+
student.Name = NameTextBox.Text;
27+
student.Grade = int.Parse(GradeTextBox.Text);
28+
29+
Close();
30+
}
31+
}
32+
}

Lesson01/Lesson01/MainWindow.xaml

+64-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,71 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
65
xmlns:local="clr-namespace:Lesson01"
7-
mc:Ignorable="d"
8-
Title="MainWindow" Height="450" Width="800">
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
Title="Lesson 01"
8+
Width="800"
9+
Height="650"
10+
WindowStartupLocation="CenterScreen"
11+
mc:Ignorable="d">
912
<Grid>
10-
13+
<Grid.ColumnDefinitions>
14+
<ColumnDefinition Width="*" />
15+
<ColumnDefinition Width="*" />
16+
</Grid.ColumnDefinitions>
17+
<Grid.RowDefinitions>
18+
<RowDefinition Height="*" />
19+
<RowDefinition Height="*" />
20+
</Grid.RowDefinitions>
21+
<TextBlock x:Name="Title"
22+
Grid.Column="0"
23+
Grid.ColumnSpan="2"
24+
Margin="324,24,0,136"
25+
HorizontalAlignment="Left"
26+
FontSize="30"
27+
FontWeight="Bold"
28+
Foreground="#4e5761"
29+
Text="Pdp Academy" />
30+
<StackPanel Grid.Row="0"
31+
Grid.Column="1"
32+
Margin="0,10,10,10"
33+
HorizontalAlignment="Right"
34+
VerticalAlignment="Bottom"
35+
Orientation="Horizontal">
36+
<Button Width="90"
37+
Height="35"
38+
Margin="0,0,10,0"
39+
HorizontalAlignment="Right"
40+
VerticalAlignment="Bottom"
41+
Background="#32a852"
42+
Click="Add_Student_Clicked"
43+
Content="Add Student"
44+
FontSize="12"
45+
Foreground="GhostWhite" />
46+
<Button Width="90"
47+
Height="35"
48+
Margin="0,0,10,0"
49+
HorizontalAlignment="Right"
50+
VerticalAlignment="Bottom"
51+
Background="#faad14"
52+
Click="Edit_Student_Clicked"
53+
Content="Edit Student"
54+
FontSize="12"
55+
Foreground="GhostWhite" />
56+
<Button Width="90"
57+
Height="35"
58+
Margin="0,0,10,0"
59+
HorizontalAlignment="Right"
60+
VerticalAlignment="Bottom"
61+
Background="#b85b53"
62+
Click="Delete_Student_Clicked"
63+
Content="Delete Student"
64+
FontSize="12"
65+
Foreground="GhostWhite" />
66+
</StackPanel>
67+
<DataGrid x:Name="StudentsDataGrid"
68+
Grid.Row="1"
69+
Grid.ColumnSpan="2"
70+
FontSize="15" />
1171
</Grid>
1272
</Window>

Lesson01/Lesson01/MainWindow.xaml.cs

+47-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
using System.Text;
1+
using Lesson01.Models;
22
using System.Windows;
3-
using System.Windows.Controls;
4-
using System.Windows.Data;
5-
using System.Windows.Documents;
6-
using System.Windows.Input;
7-
using System.Windows.Media;
8-
using System.Windows.Media.Imaging;
9-
using System.Windows.Navigation;
10-
using System.Windows.Shapes;
113

124
namespace Lesson01
135
{
@@ -16,9 +8,55 @@ namespace Lesson01
168
/// </summary>
179
public partial class MainWindow : Window
1810
{
11+
List<Student> students;
1912
public MainWindow()
2013
{
2114
InitializeComponent();
15+
16+
students = new List<Student>();
17+
students.Add(new Student(1, "Shoxbox", 2));
18+
students.Add(new Student(2, "Javlon", 1));
19+
students.Add(new Student(3, "Davlatshox", 4));
20+
21+
StudentsDataGrid.ItemsSource = students;
22+
}
23+
24+
private void Add_Student_Clicked(object sender, RoutedEventArgs e)
25+
{
26+
var addModal = new AddStudentModal(students);
27+
28+
addModal.ShowDialog();
29+
30+
StudentsDataGrid.ItemsSource = null;
31+
StudentsDataGrid.ItemsSource = students;
32+
}
33+
34+
private void Edit_Student_Clicked(object sender, RoutedEventArgs e)
35+
{
36+
if (StudentsDataGrid.SelectedItem is not Student selectedStudent)
37+
{
38+
return;
39+
}
40+
41+
var editModal = new EditStudentModal(selectedStudent);
42+
43+
editModal.ShowDialog();
44+
45+
StudentsDataGrid.ItemsSource = null;
46+
StudentsDataGrid.ItemsSource = students;
47+
}
48+
49+
private void Delete_Student_Clicked(object sender, RoutedEventArgs e)
50+
{
51+
if (StudentsDataGrid.SelectedItem is not Student selectedStudent)
52+
{
53+
return;
54+
}
55+
56+
students.Remove(selectedStudent);
57+
58+
StudentsDataGrid.ItemsSource = null;
59+
StudentsDataGrid.ItemsSource = students;
2260
}
2361
}
2462
}

Lesson01/Lesson01/Models/Student.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Lesson01.Models
2+
{
3+
public class Student
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public int Grade { get; set; }
8+
9+
public Student(int id, string name, int grade)
10+
{
11+
Id = id;
12+
Name = name;
13+
Grade = grade;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)