-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
171 lines (138 loc) · 5.16 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WpfBasics.Enums;
using WpfBasics.Models;
namespace WpfBasics
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ClickResetButton(object sender, RoutedEventArgs e)
{
var ckList = FindVisualChildren<CheckBox>(OurMainWindow);
foreach (CheckBox checkBox in ckList)
{
checkBox.IsChecked = false;
}
var tbList = FindVisualChildren<TextBox>(OurMainWindow);
foreach (TextBox textBox in tbList)
{
textBox.Text = "";
}
}
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
private void ClickAddButton(object sender, RoutedEventArgs e)
{
var auto = CreateCar();
var filename = AppDomain.CurrentDomain.BaseDirectory + @"..\..\CarFiles\" + auto.Name + ".txt";
Helpers.Serializer.SerializeObject<Car>(auto, filename);
MessageBox.Show("chyba poszło");
}
private Car CreateCar()
{
var CreatedCar = new Car
{
Name = DescriptionText.Text,
Revision = Revision.Text,
Status = Status.Text
};
if (IdNumber.Text != "")
{
CreatedCar.IdentificationNumber = Int32.Parse(IdNumber.Text.Trim());
}
if (Material.Text != "")
{
CreatedCar.Material = (MaterialEnum)System.Enum.Parse(typeof(MaterialEnum), Material.
Text.Replace(" ", "").Replace(":", ""));
}
var productionOptionList = new List<ProductionOptionEnum>();
var ckList = FindVisualChildren<CheckBox>(OurMainWindow).Where(ck => ck.IsChecked == true);
foreach (CheckBox checkBox in ckList)
{
productionOptionList.Add((ProductionOptionEnum)System.Enum.Parse(typeof(ProductionOptionEnum), ((string)(checkBox.Content))));
}
CreatedCar.ProductionOptions = productionOptionList;
if (LengthText.Text != "")
{
CreatedCar.Length = Double.Parse(LengthText.Text.Trim());
}
if (MassText.Text != "")
{
CreatedCar.Weight = Double.Parse(MassText.Text.Trim());
}
CreatedCar.Finish = (FinishEnum)System.Enum.Parse(typeof(FinishEnum), FinishDropdown.
Text.Replace(" ", "").Replace(":", ""));
CreatedCar.PurchaseInformation = (PurchaseInformationEnum)System.Enum.
Parse(typeof(PurchaseInformationEnum), PurchaseInformation.Text.Replace(" ", "").Replace(":", ""));
CreatedCar.SupplierName = SupplierNameText.Text;
if (SupplierCode.Text != "")
{
CreatedCar.SupplierCode = Int32.Parse(SupplierCode.Text.Trim());
}
CreatedCar.Note = NoteText.Text;
return CreatedCar;
}
private void ClickFindButton(object sender, RoutedEventArgs e)
{
var auto = FindCarFile();
FillFieldsInApp(auto);
}
private void FillFieldsInApp(Car car)
{
DescriptionText.Text = car.Name;
Revision.Text = car.Revision;
Status.Text = car.Status;
LengthText.Text = car.Length.ToString();
var checkboxes = FindVisualChildren<CheckBox>(OurMainWindow).ToList();
foreach (var chckbox in car.ProductionOptions)
{
var checkboxName = chckbox.ToString();
var ckbx = checkboxes.Single(ck => ck.Name.Replace("Checkbox", "") == checkboxName);
ckbx.IsChecked = true;
}
}
private Car FindCarFile()
{
var car = new Car();
var searchedText = AppDomain.CurrentDomain.BaseDirectory + @"..\..\CarFiles\" + DescriptionText.Text + ".txt";
if (File.Exists(searchedText))
{
car = Helpers.Serializer.DeSerializeObject<Car>(searchedText);
}
else
{
MessageBox.Show("There is no such car");
}
return car;
}
}
}