Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="WpfApp1.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"
- mc:Ignorable="d"
- Title="MainWindow" SizeToContent="WidthAndHeight" PreviewKeyDown="Window_PreviewKeyDown">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="300"/>
- <ColumnDefinition Width="150"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="30"/>
- <RowDefinition Height="300"/>
- </Grid.RowDefinitions>
- <ComboBox Grid.Row="0" Grid.Column="0" Name="cmb1" DisplayMemberPath="Name" IsEditable="True" IsTextSearchEnabled="False" DataObject.Pasting="cmb_Pasting" PreviewKeyDown="cmb_PreviewKeyDown" PreviewTextInput="cmb_PreviewTextInput">
- <ComboBox.ItemContainerStyle>
- <Style TargetType="ComboBoxItem">
- <EventSetter Event="PreviewMouseLeftButtonUp" Handler="ComboBoxItem_PreviewMouseLeftButtonUp"/>
- </Style>
- </ComboBox.ItemContainerStyle>
- </ComboBox>
- <ComboBox Grid.Row="0" Grid.Column="1" Name="cmb2" DisplayMemberPath="Name" IsEditable="True" IsTextSearchEnabled="False" DataObject.Pasting="cmb_Pasting" PreviewKeyDown="cmb_PreviewKeyDown" PreviewTextInput="cmb_PreviewTextInput">
- <ComboBox.ItemContainerStyle>
- <Style TargetType="ComboBoxItem">
- <EventSetter Event="PreviewMouseLeftButtonUp" Handler="ComboBoxItem_PreviewMouseLeftButtonUp"/>
- </Style>
- </ComboBox.ItemContainerStyle>
- </ComboBox>
- <TextBox Grid.Row="1" Grid.Column="0" x:Name="info"/>
- </Grid>
- </Window>
- using System;
- using System.Collections;
- 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.Navigation;
- using System.Windows.Shapes;
- namespace WpfApp1
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- List<Person> persons;
- List<NameClass> names;
- List<StreetClass> streets;
- string currInput = string.Empty;
- public MainWindow()
- {
- InitializeComponent();
- names = new List<NameClass>
- {
- new NameClass("123", "Кацевалов Артем"),
- new NameClass("456", "Гавриленко Клим"),
- new NameClass("789", "Плаксин Владислав"),
- new NameClass("000", "Исаков Владислав"),
- };
- streets = new List<StreetClass>
- {
- new StreetClass("123", "Смирнова"),
- new StreetClass("456", "Давыдова"),
- new StreetClass("789", "Победы"),
- new StreetClass("000", "Арбат"),
- };
- persons = new List<Person>
- {
- new Person("Кацевалов Артем", "Смирнова"),
- new Person("Гавриленко Клим", "Арбат"),
- new Person("Исаков Владислав", "Давыдова"),
- new Person("Гавриленко Клим", "Смирнова"),
- new Person("Плаксин Владислав", "Давыдова"),
- new Person("Кацевалов Артем", "Победы")
- };
- cmb1.ItemsSource = names;
- cmb2.ItemsSource = streets;
- cmb1.Items.Refresh();
- cmb2.Items.Refresh();
- }
- public static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
- {
- if (depObj == null) return null;
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
- {
- var child = VisualTreeHelper.GetChild(depObj, i);
- var result = (child as T) ?? GetChildOfType<T>(child);
- if (result != null) return result;
- }
- return null;
- }
- private void cmb_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Back || e.Key == Key.Delete)
- {
- ComboBox cmb = (ComboBox)sender;
- if (!string.IsNullOrEmpty(cmb.Text) && cmb.Text.Length > 1 && cmb.Text.Length > GetChildOfType<TextBox>(cmb).SelectedText.Length)
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names.Where(p => p.Name.IndexOf(cmb.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- else if (cmb == cmb2)
- cmb.ItemsSource = streets.Where(p => p.Name.IndexOf(cmb.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- cmb.IsDropDownOpen = true;
- }
- else
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names;
- else if (cmb == cmb2)
- cmb.ItemsSource = streets;
- cmb.IsDropDownOpen = false;
- cmb.SelectedItem = null;
- }
- //cmb.Focus();
- }
- }
- private void FilterRequest()
- {
- Person per;
- try
- {
- per = persons.Where(p => p.Name.Equals((cmb1.SelectedItem as NameClass)?.Name) && p.Street == (cmb2.SelectedItem as StreetClass)?.Name).ToList()[0];
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- info.Clear();
- return;
- }
- info.Text = $"\n\n\n\n\nName: {per?.Name}\nStreet: {per?.Street}";
- }
- private void cmb_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- ComboBox cmb = (ComboBox)sender;
- cmb.IsDropDownOpen = true;
- if (!string.IsNullOrEmpty(cmb.Text) && !(GetChildOfType<TextBox>(cmb).SelectedText.Length == cmb.Text.Length))
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names.Where(p => p.Name.IndexOf(cmb.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- else if (cmb == cmb2)
- cmb.ItemsSource = streets.Where(p => p.Name.IndexOf(cmb.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- cmb.IsDropDownOpen = true;
- currInput = cmb.Text.Insert(GetChildOfType<TextBox>(cmb).CaretIndex, e.Text);
- }
- else if (!string.IsNullOrEmpty(e.Text))
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names.Where(p => p.Name.IndexOf(e.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- else if (cmb == cmb2)
- cmb.ItemsSource = streets.Where(p => p.Name.IndexOf(e.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- }
- else
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names;
- else if (cmb == cmb2)
- cmb.ItemsSource = streets;
- }
- }
- private void cmb_Pasting(object sender, DataObjectPastingEventArgs e)
- {
- ComboBox cmb = (ComboBox)sender;
- cmb.IsDropDownOpen = true;
- string pastedText = (string)e.DataObject.GetData(typeof(string));
- currInput = cmb.Text.Insert(GetChildOfType<TextBox>(cmb).CaretIndex, pastedText);
- if (!string.IsNullOrEmpty(currInput))
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names.Where(p => p.Name.IndexOf(currInput, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- else if (cmb == cmb2)
- cmb.ItemsSource = streets.Where(p => p.Name.IndexOf(currInput, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
- }
- else
- {
- if (cmb == cmb1)
- cmb.ItemsSource = names;
- else if (cmb == cmb2)
- cmb.ItemsSource = streets;
- }
- }
- private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if ((e.Key == Key.Down || e.Key == Key.Up) && e.Source is ComboBox)
- {
- (e.Source as ComboBox).IsDropDownOpen = true;
- }
- else if ((e.Key == Key.Enter || e.Key == Key.Return) && e.Source is ComboBox)
- {
- if ((e.Source as ComboBox).SelectedItem == null) return;
- currInput = (e.Source as ComboBox).Text;
- FilterRequest();
- }
- }
- private void ComboBoxItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if ((sender as ComboBoxItem).DataContext is NameClass)
- {
- cmb1.SelectedItem = names.Where(p => p.ID == ((sender as ComboBoxItem).DataContext as NameClass)?.ID).ToList()[0];
- currInput = cmb1.Text;
- }
- else if ((sender as ComboBoxItem).DataContext is StreetClass)
- {
- cmb2.SelectedItem = streets.Where(a => a.ID == ((sender as ComboBoxItem).DataContext as StreetClass)?.ID).ToList()[0];
- currInput = cmb2.Text;
- }
- FilterRequest();
- }
- }
- public class Person
- {
- public string Name { get; set; }
- public string Street { get; set; }
- public Person(string n, string s) { Name = n; Street = s; }
- }
- public class NameClass
- {
- public string ID { get; set; }
- public string Name { get; set; }
- public NameClass(string i, string n) { ID = i; Name = n; }
- }
- public class StreetClass
- {
- public string ID { get; set; }
- public string Name { get; set; }
- public StreetClass(string i, string n) { ID = i; Name = n; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement