1
0
Files
cpo-lab/Lab2/src/Form/CreateScythe.xaml.cs
2024-05-02 23:19:10 +03:00

97 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}
}