설명 없음

abolshakova 8ea07c6912 Обновить 'README.md' 6 달 전
bin 6420585955 first commit 6 달 전
img 6420585955 first commit 6 달 전
models 6420585955 first commit 6 달 전
obj 6420585955 first commit 6 달 전
App.xaml 6420585955 first commit 6 달 전
App.xaml.cs 6420585955 first commit 6 달 전
AssemblyInfo.cs 6420585955 first commit 6 달 전
FileName.cs 6420585955 first commit 6 달 전
MainWindow.xaml 6420585955 first commit 6 달 전
MainWindow.xaml.cs 6420585955 first commit 6 달 전
README.md 8ea07c6912 Обновить 'README.md' 6 달 전
wpf_data.csproj 6420585955 first commit 6 달 전
wpf_data.csproj.user 6420585955 first commit 6 달 전

README.md

Получение данных из внешних источников. CSV.

Mainwindow.xamal.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_data.models;
using System.Threading.Tasks;
using System;
using System.Formats.Asn1;
using System.Globalization;
using System.IO;


namespace wpf_data
{

    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 CSVDataProvider();
            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.xamal

<Window x:Class="wpf_data.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_data"
        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}"/>
                <DataGridTextColumn
            Header="Фаворит"
            Binding="{Binding IsFavorite}"/>
                <DataGridTextColumn
            Header="Дата"
            Binding="{Binding dateOfConnections,StringFormat='dd.MM.yyyy'}"/>
            </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_data.models.Globals;
using System.Threading.Tasks;
using System.Collections.Generic;
using wpf_data;

namespace wpf_data.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 bool IsFavorite { get; set; }
        public List<PeopleAge> PeopleAgeList { get; set; }
        public List<PeopleGender> PeopleGenderList { get; set; }
        public List<PeoplePlace> PeoplePlaceList { get; set; }
        public DateOnly dateOfConnections { get; set; }

    }

}

Class2.cs

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



namespace wpf_data.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_data.models.Globals;

namespace wpf_data.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; }

    }
}

Class4.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static wpf_data.models.Globals;
using System.Threading.Tasks;
using System.Collections.Generic;
using wpf_data;
using CsvHelper;
using System.Diagnostics;
using wpf_data.models;
using static wpf_data.MainWindow;
using System.IO;
using System.Globalization;

namespace wpf_data.models
{
    public class CSVDataProvider : LocalDataProvider,IDataProvider
    {
        private List<People> PeopleList;

        // конструктор класса
        public CSVDataProvider()
        {
            using (var reader = new StreamReader("./data.csv"))
            {
                using (var csv = new CsvReader(
                    reader,
                    CultureInfo.InvariantCulture))
                {
                    PeopleList = csv.GetRecords<People>().ToList();
                }
            }
        }
        public IEnumerable<People> getPeople()
        {
            return PeopleList;
        }
    }
}

data.csv

Name,Age,Price,Gender,Place,IsFavorite,dateOfConnections
"Марина",19,500,"Ж","Набережная",True,2020-01-31
"Вика",21,9600,"Ж","Йолка",True,2011-11-11
"Кристина",17,50,"Ж","Планета",True,2019-10-12
"Анджелика",16,2000,"Ж","Бульвар",True,2020-05-05
"Костя",15,1500,"М","Бульвар",False,2022-01-25
"Вероника",25,5000,"Ж","Набережная",True,2015-01-12
"Савелий",20,1000,"М","Пляж",False,2024-03-05
"Александр",39,1500,"М","ЙОТК",False,2023-04-20
"Евгений",18,500,"М","Медведево",False,2016-09-26
"Давид",18,2500,"М","Ремзавод",False,2020-10-01