abolshakova ba5308d34a Обновить 'README.md' | 6 meses atrás | |
---|---|---|
.vs | 6 meses atrás | |
Windows | 6 meses atrás | |
bin | 6 meses atrás | |
img | 6 meses atrás | |
models | 6 meses atrás | |
obj | 6 meses atrás | |
App.xaml | 6 meses atrás | |
App.xaml.cs | 6 meses atrás | |
AssemblyInfo.cs | 6 meses atrás | |
MainWindow.xaml | 6 meses atrás | |
MainWindow.xaml.cs | 6 meses atrás | |
README.md | 6 meses atrás | |
WpfAppA.csproj | 6 meses atrás | |
WpfAppA.csproj.user | 6 meses atrás | |
dark.xaml | 6 meses atrás | |
light.xaml | 6 meses atrás | |
wpf_listbox.csproj | 6 meses atrás | |
wpf_listbox.csproj.user | 6 meses atrás | |
wpf_listbox.sln | 6 meses atrás |
MainWindow.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using wpf_listbox.models;
using System.Threading.Tasks;
using System;
using System.Formats.Asn1;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Windows.Media;
using wpf_listbox.Windows;
namespace wpf_listbox
{
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 = "Место" });
Style buttonStyle = new Style();
buttonStyle.Setters.Add(
new Setter
{
Property = Control.FontFamilyProperty,
Value = new FontFamily("Verdana")
});
buttonStyle.Setters.Add(
new Setter
{
Property = Control.MarginProperty,
Value = new Thickness(10)
});
buttonStyle.Setters.Add(
new Setter
{
Property = Control.BackgroundProperty,
Value = new SolidColorBrush(Colors.LightGoldenrodYellow)
});
buttonStyle.Setters.Add(
new Setter
{
Property = Control.ForegroundProperty,
Value = new SolidColorBrush(Colors.DarkGreen)
});
button1.Style = buttonStyle;
button2.Style = buttonStyle;
}
private string currentStyle = "StackStyle";
private void PeopleListBox_MouseDoubleClick(
object sender,
MouseButtonEventArgs e)
{
// в создаваемое окно передаем выбранного котика
var detailWindow = new DetailWindow(
PeopleListBox.SelectedItem as People);
detailWindow.ShowDialog();
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private string searchFilter = "";
private void Button_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = (Button)sender;
MessageBox.Show(clickedButton.Content.ToString());
}
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="Ремзавод"
},
};
}
IEnumerable<models.People> IDataProvider.getPeople()
{
throw new NotImplementedException();
}
}
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_listbox.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_listbox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="880" >
<Window.Resources>
<BitmapImage
x:Key='defaultImage'
UriSource='./img/1.webp' />
<Style
TargetType="Button">
<Setter
Property="FontFamily"
Value="Verdana" />
<Setter
Property="Background"
Value="PapayaWhip" />
<Setter
Property="Foreground"
Value="Peru" />
<Setter
Property="Margin"
Value="10" />
<EventSetter
Event="Button.Click"
Handler="Button_Click" />
</Style>
</Window.Resources>
<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.webp"
Grid.RowSpan="2" HorizontalAlignment="Right"/>
<StackPanel
x:Name="buttonsStack"
Background="NavajoWhite" >
<Button
x:Name="button1"
Content="Кнопка 1" />
<Button
x:Name="button2"
Content="Кнопка 2" />
</StackPanel>
<ListBox
Grid.Column="1"
Grid.Row="1"
Background="White"
ItemsSource="{Binding PeopleList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
MouseDoubleClick="PeopleListBox_MouseDoubleClick"
x:Name="PeopleListBox">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel
HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border
BorderThickness="1"
BorderBrush="Black"
CornerRadius="5">
<Grid Width="200"
Margin="10"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Image
Width="64"
Height="64"
Source="{Binding ImageBitmap,TargetNullValue={StaticResource defaultImage}}" />
<StackPanel
Grid.Column="1"
Margin="5"
Orientation="Vertical">
<TextBlock
Text="{Binding Name}"/>
<TextBlock
Text="{Binding Price}"/>
<TextBlock
Text="{Binding Place}"/>
</StackPanel>
<TextBlock
Grid.Column="2"
Text="{Binding Age}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<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>
DetwilWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using wpf_listbox.models;
namespace wpf_listbox.Windows
{
public partial class DetailWindow : Window
{
public People currentPeople { get; set; }
// конструктор класса окна
public DetailWindow(People currentPeople)
{
// и инициализируем в конструкторе
this.currentPeople = currentPeople;
InitializeComponent();
DataContext = this.currentPeople;
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
}
DetwilWindow.xaml
<Window x:Class="wpf_listbox.Windows.DetailWindow"
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_listbox.Windows"
mc:Ignorable="d"
Title="Экскурсовод" Height="auto" Width="auto">
<Window.Resources>
<BitmapImage
x:Key='defaultImage'
UriSource='./img/1.webp' />
<Style
TargetType="Button">
<Setter
Property="FontFamily"
Value="Verdana" />
<Setter
Property="Background"
Value="PapayaWhip" />
<Setter
Property="Foreground"
Value="Peru" />
<Setter
Property="Margin"
Value="10" />
</Style>
</Window.Resources>
<Grid RenderTransformOrigin="0.498,0.573">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<StackPanel
Grid.Column="1">
<TextBlock Text="Имя:">
<Run Text="{Binding Name}"/>
</TextBlock>
<TextBlock Text="Цена за экскурсию:">
<Run Text="{Binding Price}"/>
</TextBlock>
<TextBlock Text="Место:">
<Run Text="{Binding Place}"/>
</TextBlock>
<TextBlock Text="Пол:">
<Run Text="{Binding Gender}"/>
</TextBlock>
<TextBlock Text="В компании с: ">
<Run Text="{Binding dateOfConnections, StringFormat='dd.MM.yyyy'}"/>
</TextBlock>
<TextBlock Text="Является ли фаворитом:">
<Run Text="{Binding IsFavorite}"/>
</TextBlock>
</StackPanel>
<Button
x:Name="ExitButton"
Grid.Column="1"
Content="Выход"
Click="ExitButton_Click"
Height="50" />
<Image Grid.Column="0" Source="{StaticResource defaultImage}" Width="100" Height="100"/>
</Grid>
</Window>