97 lines
2.8 KiB
C#
97 lines
2.8 KiB
C#
using System;
|
||
using System.Globalization;
|
||
using System.Windows;
|
||
|
||
namespace Lab2
|
||
{
|
||
/// <summary>
|
||
/// Interaction logic for CreateScythe.xaml
|
||
/// </summary>
|
||
public partial class CreateScythe : Window
|
||
{
|
||
/// <summary>
|
||
/// Определяет, сохранять ли изменения
|
||
/// </summary>
|
||
private bool _save;
|
||
/// <summary>
|
||
/// Коса для редактирования
|
||
/// </summary>
|
||
private Scythe _scythe;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="parScythe">Коса для редактирования</param>
|
||
/// <exception cref="ArgumentNullException">Выбрасывается, когда коса равна null</exception>
|
||
public CreateScythe(Scythe parScythe)
|
||
{
|
||
if (parScythe == null)
|
||
throw new ArgumentNullException(nameof(parScythe));
|
||
|
||
InitializeComponent();
|
||
|
||
_materialComboBox.ItemsSource = Enum.GetValues(typeof(Material));
|
||
_bladeTypeComboBox.ItemsSource = Enum.GetValues(typeof(BladeType));
|
||
|
||
_scythe = parScythe;
|
||
|
||
Title = $"Scythe {_scythe.Id}";
|
||
SyncFields();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Синхронизирует поля формы с данными
|
||
/// </summary>
|
||
private void SyncFields()
|
||
{
|
||
_nameTextBox.Text = _scythe.Name;
|
||
_materialComboBox.SelectedItem = _scythe.Material;
|
||
_weightTextBox.Text = _scythe.Weight.ToString(CultureInfo.CurrentCulture);
|
||
_handleLengthTextBox.Text = _scythe.HandleLength.ToString(CultureInfo.CurrentCulture);
|
||
_bladeTypeComboBox.SelectedItem = _scythe.BladeType;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Синхронизирует данные с полей формы
|
||
/// </summary>
|
||
private void SyncData()
|
||
{
|
||
_scythe.Name = _nameTextBox.Text;
|
||
_scythe.Material = (Material)_materialComboBox.SelectedItem;
|
||
_scythe.Weight = Convert.ToDouble(_weightTextBox.Text, CultureInfo.CurrentCulture);
|
||
_scythe.HandleLength = (float)Convert.ToDouble(_handleLengthTextBox.Text, CultureInfo.CurrentCulture);
|
||
_scythe.BladeType = (BladeType)_bladeTypeComboBox.SelectedItem;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработчик события нажатия на кнопку "Save"
|
||
/// </summary>
|
||
private void SaveButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
_save = true;
|
||
|
||
Close();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработчик события закрытия окна
|
||
/// </summary>
|
||
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||
{
|
||
if (!_save)
|
||
return;
|
||
|
||
try
|
||
{
|
||
SyncData();
|
||
DialogResult = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
e.Cancel = true;
|
||
}
|
||
}
|
||
}
|
||
}
|