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


using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Questionnaire
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            cmbGender.Items.Add("Мужской");
            cmbGender.Items.Add("Женский");

            cmbCity.Items.Add("Москва");
            cmbCity.Items.Add("СПб");
            cmbCity.Items.Add("Казань");
            cmbCity.Items.Add("Новосибирск");
            cmbCity.Items.Add("Другой");
        }

        private void cmbCity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbCity.SelectedItem?.ToString() == "Другой")
                txtOtherCity.Visibility = Visibility.Visible;
            else
                txtOtherCity.Visibility = Visibility.Collapsed;
        }

        private void dpBirthDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dpBirthDate.SelectedDate.HasValue)
            {
                int age = DateTime.Today.Year - dpBirthDate.SelectedDate.Value.Year;

                if (dpBirthDate.SelectedDate.Value.Date > DateTime.Today.AddYears(-age))
                    age--;

                txtAge.Text = age.ToString();
            }
        }

        private void Show_Click(object sender, RoutedEventArgs e)
        {
            bool isValid = true;

            HighlightError(txtFIO, false);
            HighlightError(txtEmail, false);
            HighlightError(txtPhone, false);

            if (txtFIO.Text.Trim().Length < 3)
            {
                HighlightError(txtFIO, true);
                isValid = false;
            }

            if (!dpBirthDate.SelectedDate.HasValue)
                isValid = false;

            int age = 0;

            if (dpBirthDate.SelectedDate.HasValue)
            {
                age = int.Parse(txtAge.Text);

                if (age < 18 || age > 100)
                    isValid = false;
            }

            if (!IsValidEmail(txtEmail.Text))
            {
                HighlightError(txtEmail, true);
                isValid = false;
            }

            string digits = new string(txtPhone.Text
                .Where(char.IsDigit)
                .ToArray());

            if (digits.Length < 10)
            {
                HighlightError(txtPhone, true);
                isValid = false;
            }

            if (!isValid)
            {
                MessageBox.Show("Проверьте правильность заполнения данных.",
                    "Ошибка",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);

                return;
            }

            lstResult.Items.Clear();

            string city = cmbCity.Text;

            if (city == "Другой")
                city = txtOtherCity.Text;

            string pcLevel = "";

            if (rbBeginner.IsChecked == true)
                pcLevel = "Начальный";
            else if (rbMiddle.IsChecked == true)
                pcLevel = "Средний";
            else if (rbAdvanced.IsChecked == true)
                pcLevel = "Продвинутый";

            lstResult.Items.Add($"ФИО: {txtFIO.Text}");
            lstResult.Items.Add($"Возраст: {age} лет");
            lstResult.Items.Add($"Пол: {cmbGender.Text}");
            lstResult.Items.Add($"Город: {city}");
            lstResult.Items.Add($"Email: {txtEmail.Text}");
            lstResult.Items.Add($"Телефон: {txtPhone.Text}");
            lstResult.Items.Add($"Рассылка: {(chkMailing.IsChecked == true ? "Да" : "Нет")}");
            lstResult.Items.Add($"Уровень ПК: {pcLevel}");
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            txtFIO.Clear();
            txtAge.Clear();
            txtEmail.Clear();
            txtPhone.Clear();
            txtOtherCity.Clear();

            dpBirthDate.SelectedDate = null;

            cmbGender.SelectedIndex = -1;
            cmbCity.SelectedIndex = -1;

            chkMailing.IsChecked = false;

            rbBeginner.IsChecked = false;
            rbMiddle.IsChecked = false;
            rbAdvanced.IsChecked = false;

            txtOtherCity.Visibility = Visibility.Collapsed;

            lstResult.Items.Clear();

            HighlightError(txtFIO, false);
            HighlightError(txtEmail, false);
            HighlightError(txtPhone, false);
        }

        private bool IsValidEmail(string email)
        {
            return email.Contains("@") &&
                   email.IndexOf("@") < email.LastIndexOf(".");
        }

        private void HighlightError(Control control, bool hasError)
        {
            if (hasError)
                control.BorderBrush = Brushes.Red;
            else
                control.ClearValue(Border.BorderBrushProperty);
        }
    }
}