Загрузка данных


using System;
using System.Collections.Generic;
using System.Windows;

namespace UnitConverter
{
    public partial class MainWindow : Window
    {
        private Dictionary<string, Dictionary<string, double>> units;

        public MainWindow()
        {
            InitializeComponent();

            units = new Dictionary<string, Dictionary<string, double>>
            {
                {
                    "Длина",
                    new Dictionary<string, double>
                    {
                        { "мм", 0.001 },
                        { "см", 0.01 },
                        { "м", 1 },
                        { "км", 1000 }
                    }
                },

                {
                    "Вес",
                    new Dictionary<string, double>
                    {
                        { "мг", 0.001 },
                        { "г", 1 },
                        { "кг", 1000 },
                        { "т", 1000000 }
                    }
                },

                {
                    "Объём",
                    new Dictionary<string, double>
                    {
                        { "мл", 0.001 },
                        { "л", 1 },
                        { "м³", 1000 }
                    }
                }
            };

            cmbCategory.ItemsSource = units.Keys;
        }

        private void cmbCategory_SelectionChanged(object sender,
                                                   System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cmbCategory.SelectedItem == null)
                return;

            string category = cmbCategory.SelectedItem.ToString();

            cmbFrom.ItemsSource = units[category].Keys;
            cmbTo.ItemsSource = units[category].Keys;

            cmbFrom.SelectedIndex = 0;
            cmbTo.SelectedIndex = 0;
        }

        private void Convert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cmbCategory.SelectedItem == null ||
                    cmbFrom.SelectedItem == null ||
                    cmbTo.SelectedItem == null)
                {
                    MessageBox.Show("Выберите категорию и единицы измерения.");
                    return;
                }

                if (!double.TryParse(txtInput.Text, out double value))
                {
                    MessageBox.Show("Введите корректное число.");
                    return;
                }

                string category = cmbCategory.SelectedItem.ToString();
                string from = cmbFrom.SelectedItem.ToString();
                string to = cmbTo.SelectedItem.ToString();

                double baseValue = value * units[category][from];
                double result = baseValue / units[category][to];

                txtResult.Text = Math.Round(result, 4).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            txtInput.Clear();
            txtResult.Clear();

            cmbCategory.SelectedIndex = -1;
            cmbFrom.ItemsSource = null;
            cmbTo.ItemsSource = null;
        }
    }
}