Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. <UserControl x:Class="CreatingControls.SearchList"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:CreatingControls"
  7. mc:Ignorable="d"
  8. d:DesignHeight="300" d:DesignWidth="300" Loaded="SearchList_OnLoaded">
  9.  
  10.  
  11. <StackPanel>
  12. <TextBox Name="txtFilter"
  13. TextChanged="txtFilter_TextChanged"
  14. Margin="5" FontSize="20" />
  15.  
  16. <TextBox IsEnabled="False" Text="Search:"
  17. FontSize="16" BorderThickness="0" />
  18.  
  19. <ListView Name="listView"
  20. SelectedValue="{Binding Path=SelectedItem}"
  21. DisplayMemberPath="Value"
  22. BorderBrush="LightGray" Margin="5" />
  23. </StackPanel>
  24.  
  25. public partial class SearchList : UserControl
  26. {
  27. #region AllItems
  28.  
  29. public List<Collection> AllItems
  30. {
  31. get { return (List<Collection>)GetValue(AllItemsProperty); }
  32. set { SetValue(AllItemsProperty, value); }
  33. }
  34.  
  35. public static readonly DependencyProperty AllItemsProperty =
  36. DependencyProperty.Register("AllItems", typeof(List<Collection>),
  37. typeof(SearchList), new PropertyMetadata(default(List<Collection>)));
  38.  
  39. #endregion
  40.  
  41. #region SelectedItem
  42.  
  43. public Collection SelectedItem
  44. {
  45. get { return (Collection)GetValue(SelectedItemProperty); }
  46. set { SetValue(SelectedItemProperty, value); }
  47. }
  48.  
  49. public static readonly DependencyProperty SelectedItemProperty =
  50. DependencyProperty.Register("SelectedItem", typeof(Collection),
  51. typeof(SearchList), new PropertyMetadata(default(Collection)));
  52.  
  53. #endregion
  54.  
  55. public SearchList()
  56. {
  57. InitializeComponent();
  58.  
  59. listView.ItemsSource = AllItems;
  60.  
  61. if (listView.ItemsSource != null)
  62. {
  63. CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource);
  64. view.Filter = ItemsFilter;
  65. }
  66. }
  67.  
  68. private void SearchList_OnLoaded(object sender, RoutedEventArgs e)
  69. {
  70. if (listView.ItemsSource == null)
  71. return;
  72.  
  73. CollectionViewSource.GetDefaultView(listView.ItemsSource).Filter = ItemsFilter;
  74. }
  75.  
  76. private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
  77. {
  78. if (listView.ItemsSource == null)
  79. return;
  80.  
  81. CollectionViewSource.GetDefaultView(listView.ItemsSource).Refresh();
  82. }
  83.  
  84. private bool ItemsFilter(object item)
  85. {
  86. if (listView.ItemsSource == null)
  87. return false;
  88.  
  89. if (String.IsNullOrEmpty(txtFilter.Text))
  90. return true;
  91.  
  92. var collectionItem = (Collection)item;
  93.  
  94. return (collectionItem.Value.StartsWith(txtFilter.Text, StringComparison.OrdinalIgnoreCase) || collectionItem.Value.StartsWith(txtFilter.Text, StringComparison.OrdinalIgnoreCase));
  95. }
  96.  
  97. }
  98.  
  99. public class Collection : INotifyPropertyChanged
  100. {
  101. private string _id;
  102. public string Id
  103. {
  104. get { return _id; }
  105. set { SetField(ref _id, value, "Id"); }
  106. }
  107.  
  108. private string _value;
  109. public string Value
  110. {
  111. get { return _value; }
  112. set { SetField(ref _value, value, "Value"); }
  113. }
  114.  
  115. public event PropertyChangedEventHandler PropertyChanged;
  116. protected virtual void OnPropertyChanged(string propertyName)
  117. {
  118. PropertyChangedEventHandler handler = PropertyChanged;
  119. if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  120. }
  121. protected bool SetField<T>(ref T field, T value, string propertyName)
  122. {
  123. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  124. field = value;
  125. OnPropertyChanged(propertyName);
  126. return true;
  127. }
  128. }
  129.  
  130. <Window x:Class="CreatingControls.MainWindow"
  131. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  132. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  133. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  134. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  135. xmlns:uControls="clr-namespace:CreatingControls"
  136. mc:Ignorable="d"
  137. d:DataContext="Models."
  138. Title="MainWindow" >
  139.  
  140. <Grid>
  141. <uControls:SearchList x:Name="slist" />
  142. </Grid>
  143.  
  144. public partial class MainWindow : Window
  145. {
  146. public static List<Collection> items { get; set; }
  147. public User SelectedUser { get; set; }
  148.  
  149. public MainWindow()
  150. {
  151. InitializeComponent();
  152.  
  153. items = new List<Collection>();
  154. items.Add(new Collection { Id = "1", Value = "A" });
  155. items.Add(new Collection { Id = "2", Value = "B" });
  156. items.Add(new Collection { Id = "3", Value = "C" });
  157. items.Add(new Collection { Id = "4", Value = "D" });
  158. items.Add(new Collection { Id = "5", Value = "E" });
  159. items.Add(new Collection { Id = "6", Value = "F" });
  160. items.Add(new Collection { Id = "7", Value = "G" });
  161. items.Add(new Collection { Id = "8", Value = "H" });
  162.  
  163. slist.AllItems = items;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement