using System;
using System.Globalization;
using System.Windows;
namespace Lab2
{
///
/// Interaction logic for CreateScythe.xaml
///
public partial class CreateScythe : Window
{
///
/// Определяет, сохранять ли изменения
///
private bool _save;
///
/// Коса для редактирования
///
private Scythe _scythe;
///
/// Конструктор
///
/// Коса для редактирования
/// Выбрасывается, когда коса равна null
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();
}
///
/// Синхронизирует поля формы с данными
///
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;
}
///
/// Синхронизирует данные с полей формы
///
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;
}
///
/// Обработчик события нажатия на кнопку "Save"
///
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
_save = true;
Close();
}
///
/// Обработчик события закрытия окна
///
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;
}
}
}
}