Geen omschrijving

abolshakova 822cfbacbd Обновить 'README.md' 6 maanden geleden
.vs c774591249 first commit 6 maanden geleden
bin c774591249 first commit 6 maanden geleden
img c774591249 first commit 6 maanden geleden
models c774591249 first commit 6 maanden geleden
obj c774591249 first commit 6 maanden geleden
App.xaml c774591249 first commit 6 maanden geleden
App.xaml.cs c774591249 first commit 6 maanden geleden
AssemblyInfo.cs c774591249 first commit 6 maanden geleden
MainWindow.xaml c774591249 first commit 6 maanden geleden
MainWindow.xaml.cs c774591249 first commit 6 maanden geleden
README.md 822cfbacbd Обновить 'README.md' 6 maanden geleden
wpf_data_json.csproj c774591249 first commit 6 maanden geleden
wpf_data_json.csproj.user c774591249 first commit 6 maanden geleden
wpf_data_json.sln c774591249 first commit 6 maanden geleden

README.md

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

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_data_json.models;
using System.Threading.Tasks;
using System;
using System.Formats.Asn1;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Json;

namespace wpf_data_json
{
    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 JSONDataProvider();
            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="Ремзавод"
            },

        };

        }


    }
    public class JSONDataProvider: LocalDataProvider, IDataProvider
    {
        private List<People> _PeopleList;

        public JSONDataProvider()
        {
            var serializer = new DataContractJsonSerializer(typeof(People[]));
            using (var sr = new StreamReader("./data.json"))
            {
                _PeopleList = ((People[])serializer.ReadObject(sr.BaseStream)).ToList();
            }
        }

        public IEnumerable<People> getPeople()
        {
            return _PeopleList;
        }
    }

}

MainWindow.xaml

<Window x:Class="wpf_data_json.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_json"
        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>

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_json.models
{

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

    class Globals
    {
        public static IDataProvider dataProvider;
        

    }
}

Class3.cs

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

namespace wpf_data_json.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_json.models.Globals;
using System.Threading.Tasks;
using System.Collections.Generic;
using wpf_data_json;
using CsvHelper;
using System.Diagnostics;
using wpf_data_json.models;
using static wpf_data_json.MainWindow;
using System.IO;
using System.Globalization;
using System.Runtime.Serialization;

namespace wpf_data_json.models
{
    [DataContract]
    public class People
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public int Age { get; set; }
        [DataMember]
        public int Price { get; set; }
        [DataMember]
        public string Gender { get; set; }
        [DataMember]
        public string Place { get; set; }
        [DataMember]
        public bool IsFavorite { get; set; }
        [DataMember(Name = "dateOfConnections")]
        private string? stringDate { get; set; }

        [IgnoreDataMember]
        public DateTime? dateOfConnections
        {
            get
            {
                return stringDate == null ? null : DateTime.Parse(stringDate);
            }
            set
            {
                stringDate = value.ToString();
            }
        }
    }
}

data.json

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