Ei kuvausta

abolshakova 2af6b12425 Обновить 'README.md' 6 kuukautta sitten
bin 725a4649fb first commit 6 kuukautta sitten
img 725a4649fb first commit 6 kuukautta sitten
models 725a4649fb first commit 6 kuukautta sitten
obj 725a4649fb first commit 6 kuukautta sitten
App.xaml 725a4649fb first commit 6 kuukautta sitten
App.xaml.cs 725a4649fb first commit 6 kuukautta sitten
AssemblyInfo.cs 725a4649fb first commit 6 kuukautta sitten
MainWindow.xaml 725a4649fb first commit 6 kuukautta sitten
MainWindow.xaml.cs 725a4649fb first commit 6 kuukautta sitten
README.md 2af6b12425 Обновить 'README.md' 6 kuukautta sitten
wpf_template.csproj 725a4649fb first commit 6 kuukautta sitten
wpf_template.csproj.user 725a4649fb first commit 6 kuukautta sitten

README.md

Поиск, сортировка

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 wpf_template.models;
using System.Threading.Tasks;
using System;


namespace wpf_template
{

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void Invalidate()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("PeopleList"));
        }
        public string selectedGender = "Пол";
        public PeopleAge? selectedAge = null;
        public string selectedPlace = "Место";

        private IEnumerable<People> _PeopleList;
        public IEnumerable<People> PeopleList
        {
            get
            {
                var res = _PeopleList;

                // фильтруем по возрасту
                res = res
                     .Where(c => (c.Gender == selectedGender || selectedGender == "Пол"))
                     .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
                     .Where(c => (c.Place == selectedPlace || selectedPlace == "Место"));
                // если фильтр не пустой, то ищем ВХОЖДЕНИЕ подстроки поиска в кличке без учета регистра
                if (searchFilter != "")
                    res = res.Where(c => c.Name.IndexOf(
                        searchFilter,
                        StringComparison.OrdinalIgnoreCase) >= 0 || c.Gender.IndexOf(
                        searchFilter,
                        StringComparison.OrdinalIgnoreCase) >= 0 || c.Place.IndexOf(
                        searchFilter,
                        StringComparison.OrdinalIgnoreCase) >= 0);
                if (sortAsc) res = res.OrderBy(c => c.Price);
                else res = res.OrderByDescending(c => c.Price);
                if (sortAsc) res = res.OrderBy(c => c.Price);
                else res = res.OrderByDescending(c => c.Price);

                return res;
            }
            set
            {
                _PeopleList = value;
            }
        }


        public List<PeopleGender> PeopleGenderList { get; set; }
        public List<PeopleAge> PeopleAgeList { get; set; }
        public List<PeoplePlace> PeoplePlaceList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Globals.dataProvider = new LocalDataProvider();
            PeopleList = Globals.dataProvider.getPeople();
            PeopleGenderList = Globals.dataProvider.getGender().ToList();
            PeopleGenderList.Insert(0, new PeopleGender { title = "Пол" });
            PeopleAgeList = Globals.dataProvider.getAge().ToList();
            PeoplePlaceList = Globals.dataProvider.getPlace().ToList();
            PeoplePlaceList.Insert(0, new PeoplePlace { title = "Место" });
        }


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

        }
        private string searchFilter = "";

        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            Invalidate();
        }

        private void GenderFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedGender = (GenderFilterComboBox.SelectedItem as PeopleGender).title;
            Invalidate();
        }
        private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedAge = AgeFilterComboBox.SelectedItem as PeopleAge;
            Invalidate();
        }
        private void PlaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedPlace = (PlaceFilterComboBox.SelectedItem as PeoplePlace).title;
            Invalidate();
        }
        private bool sortAsc = true;

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = (sender as RadioButton).Tag.ToString() == "1";
            Invalidate();
        }


        public class LocalDataProvider : IDataProvider
        {
            public IEnumerable<PeopleGender> getGender()
            {
                return new PeopleGender[]
                {
                new PeopleGender()
                {
                    title="Ж"

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

                },



                new PeopleAge()
                {
                    title="Подростки",
                    AgeFrom=15,
                    AgeTo=18

                },
                new PeopleAge()
                {
                    title="Молодые",
                    AgeFrom=18,
                    AgeTo=30

                },
                new PeopleAge()
                {
                    title="В Возврасте",
                    AgeFrom=30,
                    AgeTo=99

                }

                };

            }
            public IEnumerable<PeoplePlace> getPlace()
            {
                return new PeoplePlace[]
                {
                new PeoplePlace()
                {
                    title="Набережная"

                },
                new PeoplePlace()
                {
                    title="Йолка"
                },
                new PeoplePlace()
                {
                    title="Бульвар"
                },
                new PeoplePlace()
                {
                    title="ЙОТК"
                },
               new PeoplePlace()
                {
                    title="Планета"
                },
               new PeoplePlace()
                {
                    title="Ремзавод"
                },
               new PeoplePlace()
                {
                    title="Медведево"
                },
               new PeoplePlace()
                {
                    title="Пляж"
                },

                };
            }
            public IEnumerable<People> getPeople()
            {
                return new People[]{
            new People
            {
                Age=19,
                Name="Марина",
                Gender="Ж",
                Price=500,
                Place="Набережная"
            },
            new People
            {
                Price=9600,
                Name="Вика",
                Age=21,
                Gender="Ж",
                Place="Йолка"
            },
            new People
            {
                Price=50,
                Name="Кристина",
                Age=17,
                Gender="Ж",
                Place="Планета"
            },
            new People
            {
                Price=2000,
                Name="Анджелика",
                Age=16,
                Gender="Ж",
                Place="Бульвар"
            },
            new People
            {
                Price=1500,
                Name="Костя",
                Age=15,
                Gender="М",
                Place="Бульвар"
            },
            new People
            {
                Price=5000,
                Name="Вероника",
                Age=25,
                Gender="Ж",
                Place="Набережная"
            },
            new People
            {
                Price=1000,
                Name="Савелий",
                Age=20,
                Gender="М",
                Place="Пляж"
            },
            new People
            {
                Price=1500,
                Name="Александр",
                Age=39,
                Gender="М",
                Place="ЙОТК"
            },
            new People
            {
                Price=500,
                Name="Евгений",
                Age=18,
                Gender="М",
                Place="Медведево"
            },
            new People
            {
                Price=2500,
                Name="Давид",
                Age=18,
                Gender="М",
                Place="Ремзавод"
            },

        };

            }


        }
    }
}

MainWindow.cs

<Window x:Class="wpf_template.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:wpf_template"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="880">
    <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/1.jpg" 
        Grid.RowSpan="2" HorizontalAlignment="Right"/>
        <DataGrid
    Grid.Row="1"
    Grid.Column="1"
    CanUserAddRows="False"
    AutoGenerateColumns="False"
    ItemsSource="{Binding PeopleList}">
            <DataGrid.Columns>
                <DataGridTextColumn
            Header="Имя"
            Binding="{Binding Name}"/>
                <DataGridTextColumn
            Header="Возраст"
            Binding="{Binding Age}"/>
                <DataGridTextColumn
            Header="Пол"
            Binding="{Binding Gender}"/>
                <DataGridTextColumn
            Header="Прайс"
            Binding="{Binding Price}"/>
                <DataGridTextColumn
            Header="Место"
            Binding="{Binding Place}"/>
            </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"/>
            <TextBox
    Width="200"
    VerticalAlignment="Center"
    x:Name="SearchFilterTextBox" 
    KeyUp="SearchFilter_KeyUp"/>

            <ComboBox
    Name="GenderFilterComboBox"
    SelectionChanged="GenderFilterComboBox_SelectionChanged_1"
    VerticalAlignment="Center"
    MinWidth="100"
    SelectedIndex="0"
    ItemsSource="{Binding PeopleGenderList}">

                <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 PeopleAgeList}">

                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label 
            Content="{Binding title}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Label 
    Content="Место:"
    VerticalAlignment="Center"/>
            <ComboBox
    Name="PlaceFilterComboBox"
    SelectionChanged="PlaceFilterComboBox_SelectionChanged_3"
    VerticalAlignment="Center"
    MinWidth="100"
    SelectedIndex="0"
    ItemsSource="{Binding PeoplePlaceList}">

                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label 
            Content="{Binding title}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Label 
    Content="Цена:" 
    VerticalAlignment="Center"/>
            <RadioButton
    GroupName="Price"
    Tag="1"
    Content="Сначала недорогие"
    IsChecked="True"
    Checked="RadioButton_Checked"
    VerticalAlignment="Center"/>
            <RadioButton
    GroupName="Price"
    Tag="2"
    Content="Сначала дорогие"
    Checked="RadioButton_Checked"
    VerticalAlignment="Center"/>
            <!-- минимальную высоту я тут поставил, чтобы верхнюю строку сетки было видно. В реальном приложении она не нужна -->
        </WrapPanel>
    </Grid>
</Window>

Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static wpf_template.models.Globals;
using System.Threading.Tasks;
using System.Collections.Generic;
using wpf_template;

namespace wpf_template.models
{
    public class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Price { get; set; }
        public string  Gender { get; set; }
        public string Place { get; set; }
        public List<PeopleAge> PeopleAgeList { get; set; }
        public List<PeopleGender> PeopleGenderList { get; set; }
        public List<PeoplePlace> PeoplePlaceList { get; set; }

    }
    
}

Class2.cs

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



namespace wpf_template.models
{

    interface IDataProvider
    {
        IEnumerable<People> getPeople();
        IEnumerable<PeopleGender> getGender();
        IEnumerable<PeopleAge> getAge();
        IEnumerable<PeoplePlace> getPlace();
    }

    class Globals
    {
        public static IDataProvider dataProvider;
        IEnumerable<PeopleGender> getPeopleGenders()
        {
            return new PeopleGender[] {
                new PeopleGender { title = "Ж"},
                new PeopleGender { title = "М"}
            };
        }
        IEnumerable<PeopleAge> getPeopleAges()
        {
            return new PeopleAge[] {
                new PeopleAge{title="Все возраста", AgeFrom=0, AgeTo=99},
                new PeopleAge{title="Подростки", AgeFrom=15, AgeTo = 18},
                new PeopleAge{title="Молодые", AgeFrom=18, AgeTo=30},
                new PeopleAge{title="В Возврасте", AgeFrom=30, AgeTo=99}
            };
        }
        IEnumerable<PeoplePlace> getPeoplePlace()
        {
            return new PeoplePlace[] {
                new PeoplePlace { title = "Набережная"},
                new PeoplePlace { title = "Бульвар"},
                new PeoplePlace { title = "Йолка"},
                new PeoplePlace { title = "Планета"},
                new PeoplePlace { title = "ЙОТК"},
                new PeoplePlace { title = "Ремзавод"},
                new PeoplePlace { title = "Медведево"},
                new PeoplePlace { title = "Пляж"},
            };
        }
    }
}

Class3.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static wpf_template.models.Globals;

namespace wpf_template.models
{
    public class PeopleGender
    {
        public string title { get; set; }
    }
    public class PeopleAge
    {
        public string title { get; set; }
        public int AgeFrom { get; set; }
        public int AgeTo { get; set; }
    }
    public class PeoplePlace
    {
        public string title { get; set; }

    }
}