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


namespace BoardGamesCatalog
{
    public class Game
    {
        public string Name { get; set; }
        public string Genre { get; set; }
        public int MinPlayers { get; set; }
        public int MaxPlayers { get; set; }
        public int AgeLimit { get; set; }
        public string Description { get; set; }

        public string Players => $"{MinPlayers}-{MaxPlayers}";
    }
}


<Window x:Class="BoardGamesCatalog.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Каталог настольных игр"
        Height="600" Width="950">

    <Grid Margin="15">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Поиск и фильтр -->
        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">

            <Label Content="Поиск:" VerticalAlignment="Center"/>
            <TextBox x:Name="SearchBox"
                     Width="200"
                     Margin="5"
                     TextChanged="SearchBox_TextChanged"/>

            <Label Content="Жанр:" VerticalAlignment="Center"
                   Margin="15,0,0,0"/>

            <ComboBox x:Name="GenreFilter"
                      Width="160"
                      Margin="5"
                      SelectionChanged="GenreFilter_SelectionChanged">
                <ComboBoxItem Content="Все жанры"/>
                <ComboBoxItem Content="Стратегия"/>
                <ComboBoxItem Content="Семейная"/>
                <ComboBoxItem Content="Карточная"/>
                <ComboBoxItem Content="Приключение"/>
                <ComboBoxItem Content="Экономическая"/>
            </ComboBox>
        </StackPanel>

        <!-- Список игр -->
        <ListView x:Name="GamesList"
                  Grid.Row="1"
                  SelectionChanged="GamesList_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Название"
                                    DisplayMemberBinding="{Binding Name}"
                                    Width="180"/>
                    <GridViewColumn Header="Жанр"
                                    DisplayMemberBinding="{Binding Genre}"
                                    Width="140"/>
                    <GridViewColumn Header="Игроки"
                                    DisplayMemberBinding="{Binding Players}"
                                    Width="100"/>
                    <GridViewColumn Header="Возраст"
                                    DisplayMemberBinding="{Binding AgeLimit}"
                                    Width="100"/>
                    <GridViewColumn Header="Описание"
                                    DisplayMemberBinding="{Binding Description}"
                                    Width="300"/>
                </GridView>
            </ListView.View>
        </ListView>

        <!-- Поля добавления -->
        <Grid Grid.Row="2" Margin="0,10,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Column="0" Margin="5">
                <Label Content="Название"/>
                <TextBox x:Name="NameBox"/>
            </StackPanel>

            <StackPanel Grid.Column="1" Margin="5">
                <Label Content="Жанр"/>
                <ComboBox x:Name="GenreBox">
                    <ComboBoxItem Content="Стратегия"/>
                    <ComboBoxItem Content="Семейная"/>
                    <ComboBoxItem Content="Карточная"/>
                    <ComboBoxItem Content="Приключение"/>
                    <ComboBoxItem Content="Экономическая"/>
                </ComboBox>
            </StackPanel>

            <StackPanel Grid.Column="2" Margin="5">
                <Label Content="Игроки (мин.)"/>
                <TextBox x:Name="MinPlayersBox"/>

                <Label Content="Игроки (макс.)"/>
                <TextBox x:Name="MaxPlayersBox"/>
            </StackPanel>

            <StackPanel Grid.Column="3" Margin="5">
                <Label Content="Возрастное ограничение"/>
                <TextBox x:Name="AgeBox"/>

                <Label Content="Описание"/>
                <TextBox x:Name="DescriptionBox"
                         Height="45"
                         TextWrapping="Wrap"/>
            </StackPanel>
        </Grid>

        <!-- Кнопки -->
        <StackPanel Grid.Row="3"
                    Orientation="Horizontal"
                    HorizontalAlignment="Center"
                    Margin="0,10,0,0">

            <Button Content="Добавить в каталог"
                    Width="160"
                    Margin="5"
                    Click="AddButton_Click"/>

            <Button Content="Изменить"
                    Width="100"
                    Margin="5"
                    Click="EditButton_Click"/>

            <Button Content="Удалить"
                    Width="100"
                    Margin="5"
                    Click="DeleteButton_Click"/>

            <Button Content="Открыть карточку"
                    Width="140"
                    Margin="5"
                    Click="OpenButton_Click"/>
        </StackPanel>
    </Grid>
</Window>



using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace BoardGamesCatalog
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<Game> games =
            new ObservableCollection<Game>();

        public MainWindow()
        {
            InitializeComponent();

            games.Add(new Game
            {
                Name = "Каркассон",
                Genre = "Стратегия",
                MinPlayers = 2,
                MaxPlayers = 5,
                AgeLimit = 8,
                Description = "Игра с размещением тайлов и развитием территории."
            });

            games.Add(new Game
            {
                Name = "Диксит",
                Genre = "Семейная",
                MinPlayers = 3,
                MaxPlayers = 6,
                AgeLimit = 8,
                Description = "Ассоциативная игра с иллюстрациями."
            });

            GamesList.ItemsSource = games;
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            if (!int.TryParse(MinPlayersBox.Text, out int min) ||
                !int.TryParse(MaxPlayersBox.Text, out int max) ||
                !int.TryParse(AgeBox.Text, out int age))
            {
                MessageBox.Show("Проверьте числовые поля.");
                return;
            }

            if (string.IsNullOrWhiteSpace(NameBox.Text) ||
                GenreBox.SelectedItem == null)
            {
                MessageBox.Show("Заполните название и жанр.");
                return;
            }

            ComboBoxItem genreItem =
                (ComboBoxItem)GenreBox.SelectedItem;

            games.Add(new Game
            {
                Name = NameBox.Text,
                Genre = genreItem.Content.ToString(),
                MinPlayers = min,
                MaxPlayers = max,
                AgeLimit = age,
                Description = DescriptionBox.Text
            });

            ClearFields();
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (GamesList.SelectedItem is Game game)
            {
                OpenGameCard(game);
            }
            else
            {
                MessageBox.Show("Выберите игру.");
            }
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            if (GamesList.SelectedItem is Game game)
            {
                OpenGameCard(game);
            }
            else
            {
                MessageBox.Show("Выберите игру.");
            }
        }

        private void OpenGameCard(Game game)
        {
            GameCardWindow window = new GameCardWindow(game);
            window.Owner = this;
            window.ShowDialog();

            GamesList.Items.Refresh();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (GamesList.SelectedItem is Game game)
            {
                MessageBoxResult result = MessageBox.Show(
                    $"Удалить игру «{game.Name}»?",
                    "Подтверждение удаления",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question);

                if (result == MessageBoxResult.Yes)
                {
                    games.Remove(game);
                }
            }
            else
            {
                MessageBox.Show("Выберите игру.");
            }
        }

        private void SearchBox_TextChanged(
            object sender, TextChangedEventArgs e)
        {
            ApplyFilter();
        }

        private void GenreFilter_SelectionChanged(
            object sender, SelectionChangedEventArgs e)
        {
            ApplyFilter();
        }

        private void ApplyFilter()
        {
            if (GamesList == null)
                return;

            string search = SearchBox.Text.ToLower();

            string genre = "Все жанры";

            if (GenreFilter.SelectedItem is ComboBoxItem item)
                genre = item.Content.ToString();

            var filtered = games.Where(g =>
                g.Name.ToLower().Contains(search) &&
                (genre == "Все жанры" || g.Genre == genre));

            GamesList.ItemsSource = filtered.ToList();
        }

        private void GamesList_SelectionChanged(
            object sender, SelectionChangedEventArgs e)
        {
            // Обработчик выбора игры
        }

        private void ClearFields()
        {
            NameBox.Clear();
            MinPlayersBox.Clear();
            MaxPlayersBox.Clear();
            AgeBox.Clear();
            DescriptionBox.Clear();
            GenreBox.SelectedIndex = -1;
        }
    }
}



<Window x:Class="BoardGamesCatalog.GameCardWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Карточка игры"
        Height="430"
        Width="400"
        WindowStartupLocation="CenterOwner">

    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Label Content="Название"/>
        <TextBox x:Name="NameBox"
                 Grid.Row="1"
                 Margin="0,0,0,10"/>

        <Label Content="Жанр"
               Grid.Row="2"/>

        <ComboBox x:Name="GenreBox"
                  Grid.Row="3"
                  Margin="0,0,0,10">
            <ComboBoxItem Content="Стратегия"/>
            <ComboBoxItem Content="Семейная"/>
            <ComboBoxItem Content="Карточная"/>
            <ComboBoxItem Content="Приключение"/>
            <ComboBoxItem Content="Экономическая"/>
        </ComboBox>

        <StackPanel Grid.Row="4"
                    Orientation="Horizontal">

            <TextBox x:Name="MinPlayersBox"
                     Width="70"
                     Margin="0,0,10,0"/>

            <TextBox x:Name="MaxPlayersBox"
                     Width="70"
                     Margin="0,0,10,0"/>

            <TextBox x:Name="AgeBox"
                     Width="70"/>
        </StackPanel>

        <TextBox x:Name="DescriptionBox"
                 Grid.Row="5"
                 Margin="0,10,0,10"
                 TextWrapping="Wrap"/>

        <StackPanel Grid.Row="6"
                    Orientation="Horizontal"
                    HorizontalAlignment="Right">

            <Button Content="Сохранить"
                    Width="100"
                    Margin="5"
                    Click="SaveButton_Click"/>

            <Button Content="Отмена"
                    Width="100"
                    Margin="5"
                    Click="CancelButton_Click"/>
        </StackPanel>
    </Grid>
</Window>



using System.Windows;
using System.Windows.Controls;

namespace BoardGamesCatalog
{
    public partial class GameCardWindow : Window
    {
        private Game game;

        public GameCardWindow(Game selectedGame)
        {
            InitializeComponent();

            game = selectedGame;

            NameBox.Text = game.Name;
            MinPlayersBox.Text = game.MinPlayers.ToString();
            MaxPlayersBox.Text = game.MaxPlayers.ToString();
            AgeBox.Text = game.AgeLimit.ToString();
            DescriptionBox.Text = game.Description;

            foreach (ComboBoxItem item in GenreBox.Items)
            {
                if (item.Content.ToString() == game.Genre)
                {
                    GenreBox.SelectedItem = item;
                    break;
                }
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (!int.TryParse(MinPlayersBox.Text, out int min) ||
                !int.TryParse(MaxPlayersBox.Text, out int max) ||
                !int.TryParse(AgeBox.Text, out int age))
            {
                MessageBox.Show("Проверьте числовые значения.");
                return;
            }

            game.Name = NameBox.Text;

            if (GenreBox.SelectedItem is ComboBoxItem item)
                game.Genre = item.Content.ToString();

            game.MinPlayers = min;
            game.MaxPlayers = max;
            game.AgeLimit = age;
            game.Description = DescriptionBox.Text;

            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}