readme.md 15 KB

Каркас приложения. Модель данных. Привязка данных. Табличный вывод.

Вот что у меня получчилось, по моей предметной области

class1

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static WpfApp2.model.Globals;
using System.Threading.Tasks;

namespace WpfApp2.model
{
    public class Hero
    {
        public string name { get; set; }
        public int Age { get; set; }
        public string power { get; set; }
        public string race { get; set; }
        public List<HeroPower> HeroPowerList { get; set; }
        public List<HeroAge> HeroAgeList { get; set; }
        public List<HeroRace> HeroRaceList { get; set; }

    }
}


class2

using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace WpfApp2.model
{
    class Globals
    {
        public static IDataProvider dataProvider;
        IEnumerable<HeroPower> getHeroPower()
        {
            return new HeroPower[] {
                new HeroPower { title = "1/10"},
                new HeroPower { title = "2/10"},
                new HeroPower { title = "3/10"},
                new HeroPower { title = "4/10"},
                new HeroPower { title = "5/10"},
                new HeroPower { title = "6/10"},
                new HeroPower { title = "7/10"},
                new HeroPower { title = "8/10"},
                new HeroPower { title = "9/10"},
                new HeroPower { title = "10/10"}
            };
        }
        IEnumerable<HeroAge> getHeroAges()
        {
            return new HeroAge[] {
                new HeroAge{title="Все возраста", AgeFrom=0, AgeTo=2000},
                new HeroAge{title="Дети", AgeFrom=1, AgeTo=100},
                new HeroAge{title="Подростки", AgeFrom=100, AgeTo = 500},
                new HeroAge{title="Мужчины", AgeFrom=500, AgeTo=700},
                new HeroAge{title="Старики", AgeFrom=700, AgeTo=200}
            };
        }
        IEnumerable<HeroRace> getHeroRace()
        {
            return new HeroRace[] {
                new HeroRace { title = "Человек"},
                new HeroRace { title = "Огр"},
                new HeroRace { title = "Дракон"},
                new HeroRace { title = "Гоблин"},
                new HeroRace { title = "Фея"},
                new HeroRace { title = "Камень"},
                new HeroRace { title = "Рыба"},
                new HeroRace { title = "Селёдка"}
            };
        }
    }
}

class3

using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2.model
{
    public class HeroPower
    {
        public string title { get; set; }
    }
    public class HeroAge
    {
        public string title { get; set; }
        public int AgeFrom { get; set; }
        public int AgeTo { get; set; }
    }

    public class HeroRace
    {
        public string title { get; set; }
        public string RaceFrom { get; set; }
        public string RaceTo { get; set; }
    }
}

Mainwindow.xaml

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition />
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <!-- типа логотип компании -->
        <Image 
        Source="img/portrait.jpg" 
        Grid.RowSpan="2" HorizontalAlignment="Right"/>
        <DataGrid
    Grid.Row="1"
    Grid.Column="1"
    CanUserAddRows="False"
    AutoGenerateColumns="False"
    ItemsSource="{Binding HeroList}">
            <DataGrid.Columns>
                <DataGridTextColumn
Header="Имя"
Binding="{Binding name}"/>
                <DataGridTextColumn
Header="Возраст"
Binding="{Binding Age}"/>
                <DataGridTextColumn
Header="Мощь"
Binding="{Binding power}"/>
                <DataGridTextColumn
Header="Раса"
Binding="{Binding race}"/>
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel 
        Orientation="Vertical"
        Grid.RowSpan="3"
        VerticalAlignment="Bottom">
            <Button 
            x:Name="ExitButton"
            Content="Выход" 
            Click="ExitButton_Click"
            Height="50"/>
        </StackPanel>

        <WrapPanel
        Orientation="Horizontal"
        Grid.Column="1"
        MinHeight="50">
            <Label 
    Content="Мощь:"
    VerticalAlignment="Center"/>

            <ComboBox
    Name="powerFilterComboBox"
    SelectionChanged="powerFilterComboBox_SelectionChanged_1"
    VerticalAlignment="Center"
    MinWidth="100"
    SelectedIndex="0"
    ItemsSource="{Binding HeroPowerList}">

                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label 
                Content="{Binding title}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Label 
Content="Возраст:"
VerticalAlignment="Center"/>
            <ComboBox
Name="AgeFilterComboBox"
SelectionChanged="AgeFilterComboBox_SelectionChanged_2"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding HeroAgeList}">

                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label 
            Content="{Binding title}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Label 
Content="Раса:"
VerticalAlignment="Center"/>
            <ComboBox
Name="RaceFilterComboBox"
SelectionChanged="RaceFilterComboBox_SelectionChanged_3"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding HeroRaceList}">

                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label 
            Content="{Binding title}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <!-- минимальную высоту я тут поставил, чтобы верхнюю строку сетки было видно. В реальном приложении она не нужна -->
        </WrapPanel>
    </Grid>
</Window>

Mainwindow.xaml.cs

using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using WpfApp2.model;
using System.Threading.Tasks;
using System;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void Invalidate()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("HeroList"));
        }
        public string selectedPower = "Мощь";
        public HeroAge? selectedAge = null;
        public string selectedRace = "Раса";

        private IEnumerable<Hero> _HeroList;
        public IEnumerable<Hero> HeroList
        {
            get
            {
                return _HeroList
                    .Where(c => (c.power == selectedPower || selectedPower == "Мощь"))
                    .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
                    .Where(c => (c.race == selectedRace || selectedRace == "Раса"));
            }
            set
            {
                _HeroList = value;
            }
        }

        public List<HeroPower> HeroPowerList { get; set; }
        public List<HeroAge> HeroAgeList { get; set; }
        public List<HeroRace> HeroRaceList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Globals.dataProvider = new LocalDataProvider();
            HeroList = Globals.dataProvider.getHero();
            HeroPowerList = Globals.dataProvider.getPower().ToList();
            HeroPowerList.Insert(0, new HeroPower { title = "Мощь" });
            HeroAgeList = Globals.dataProvider.getAge().ToList();
            HeroRaceList = Globals.dataProvider.getRace().ToList();
            HeroRaceList.Insert(0, new HeroRace { title = "Раса" });
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();

        }

        private void powerFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedPower = (powerFilterComboBox.SelectedItem as HeroPower).title;
            Invalidate();
        }
        private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedAge = AgeFilterComboBox.SelectedItem as HeroAge;
            Invalidate();
        }
        private void RaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedRace = (RaceFilterComboBox.SelectedItem as HeroRace).title;
            Invalidate();
        }
    }
    interface IDataProvider
    {
        IEnumerable<Hero> getHero();
        IEnumerable<HeroPower> getPower();
        IEnumerable<HeroAge> getAge();
        IEnumerable<HeroRace> getRace();
    }

    public class LocalDataProvider : IDataProvider
    {
        public IEnumerable<HeroPower> getPower()
        {
            return new HeroPower[]
            {
                new HeroPower()
                {
                    title="1/10"

                },
                new HeroPower()
                {
                    title="2/10"
                },
                new HeroPower()
                {
                    title="3/10"

                },
                new HeroPower()
                {
                    title="4/10"

                },
                new HeroPower()
                {
                    title="5/10"

                },
                new HeroPower()
                {
                    title="6/10"

                },
                new HeroPower()
                {
                    title="7/10"

                },
                new HeroPower()
                {
                    title="8/10"

                },
                new HeroPower()
                {
                    title="9/10"

                },
                new HeroPower()
                {
                    title="10/10"

                },

            };
        }
        public IEnumerable<HeroAge> getAge()
        {
            return new HeroAge[]
            {
                new HeroAge()
                {
                    title="Все возраста",
                    AgeFrom=0,
                    AgeTo=2000

                },
                new HeroAge()
                {
                    title="Дети",
                    AgeFrom=1,
                    AgeTo=100

                },
                new HeroAge()
                {
                    title="Подростки",
                    AgeFrom=100,
                    AgeTo=500

                },
                new HeroAge()
                {
                    title="Молодые",
                    AgeFrom=500,
                    AgeTo=700

                },
                new HeroAge()
                {
                    title="Старики",
                    AgeFrom=700,
                    AgeTo=2000

                }

            };

        }
        public IEnumerable<HeroRace> getRace()
        {
            return new HeroRace[]
            {
                new HeroRace()
                {
                    title="Человек"
                },
                new HeroRace()
                {
                    title="Дракон"
                },
                new HeroRace()
                {
                    title="Фея"
                },
                new HeroRace()
                {
                    title="Огр"
                },
                new HeroRace()
                {
                    title="Рыба"
                },
                new HeroRace()
                {
                    title="Селёдка"
                },
                new HeroRace()
                {
                    title="Камень"
                },
                new HeroRace()
                {
                    title="Гоблин"
                },
            };
        }
            public IEnumerable<Hero> getHero()
            {
                return new Hero[]{
            new Hero{
                race="Человек",
                name="Invoker",
                Age=1000,
                power="10/10"},
            new Hero{
                race="Человек",
                name="Juggernaut",
                Age=450,
                power="9/10"},
            new Hero{
                race="Человек",
                name="lina",
                Age = 300,
                power="8/10"},
            new Hero{
                race="Огр",
                name="Troll Warloard",
                Age = 100,
                power="7/10"},
            new Hero{
                race="Рыба",
                name="Slark",
                Age = 120,
                power="6/10"},
            new Hero{
                race="Селёдка",
                name="Slardar",
                Age = 200,
                power="5/10"},
            new Hero{
                race="Камень",
                name="Tiny",
                Age = 20000,
                power="4/10"},
            new Hero{
                race="Фея",
                name="Dark Willow",
                Age = 700,
                power="3/10"},
            new Hero{
                race="Огр",
                name="Orge Magi",
                Age = 1400,
                power="2/10"},
            new Hero{
                race="Дракон",
                name="Viper",
                Age = 1900,
                power="1/10"},

        };


            }


        }

    }

То что получилосьв итоге ^__^