■ ListView 클래스의 ItemsSource 속성에서 필터 처리한 컬렉션을 설정하는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ Contact.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
namespace TestProject; /// <summary> /// 연락처 /// </summary> public class Contact { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; private set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; private set; } #endregion #region 회사 - Company /// <summary> /// 회사 /// </summary> public string Company { get; private set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name => $"{FirstName} {LastName}"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Contact(firstName, lastName, company) /// <summary> /// 생성자 /// </summary> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> /// <param name="company">회사</param> public Contact(string firstName, string lastName, string company) { FirstName = firstName; LastName = lastName; Company = company; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return Name; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="10"> <TextBox Name="filterTextBox" HorizontalAlignment="Left" Width="200" Header="Filter by Last Name" /> <ListView Name="listView" HorizontalAlignment="Center" Width="300" Height="300" BorderThickness="1" BorderBrush="DarkGray" Padding="5" /> </StackPanel> </Grid> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 연락처 리스트 /// </summary> private IList<Contact> sourceContactList = new List<Contact>(); /// <summary> /// 타겟 연락처 컬렉션 /// </summary> private ObservableCollection<Contact> targetContectCollection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.sourceContactList.Add(new Contact("Kendall" , "Collins", "Adatum Corporation")); this.sourceContactList.Add(new Contact("Victoria", "Burke" , "Bellows College" )); this.sourceContactList.Add(new Contact("Preston" , "Morales", "Margie's Travel" )); this.sourceContactList.Add(new Contact("Miguel" , "Reyes" , "Tailspin Toys" )); this.targetContectCollection = new ObservableCollection<Contact>(this.sourceContactList); this.listView.ItemsSource = this.targetContectCollection; this.filterTextBox.TextChanged += filterTextBox_TextChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 필터 텍스트 박스 텍스트 변경시 처리하기 - filterTextBox_TextChanged(sender, e) /// <summary> /// 필터 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">"이벤트 인자></param> private void filterTextBox_TextChanged(object sender, TextChangedEventArgs e) { string filterString = (sender as TextBox).Text; IEnumerable<Contact> filteredContactEnumerable = this.sourceContactList.Where(contact => Filter(contact, filterString)); RemoveContactNotInFilteredContactEnumerable(filteredContactEnumerable); AddContactNotInTargetContactCollection(filteredContactEnumerable); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 필터 처리하기 - Filter(contact, filterString) /// <summary> /// 필터 처리하기 /// </summary> /// <param name="contact">연락처</param> /// <param name="filterString">필터 문자열</param> /// <returns>필터 처리 여부</returns> private bool Filter(Contact contact, string filterString) { return contact.LastName.Contains(filterString, StringComparison.InvariantCultureIgnoreCase); } #endregion #region 필터 연락처 열거 가능형에서 없는 연락처 제거하기 - RemoveContactNotInFilteredContactEnumerable(filteredContactEnumerable) /// <summary> /// 필터 연락처 열거 가능형에서 없는 연락처 제거하기 /// </summary> /// <param name="filteredContactEnumerable">필터 연락처 열거 가능형</param> private void RemoveContactNotInFilteredContactEnumerable(IEnumerable<Contact> filteredContactEnumerable) { for(int i = this.targetContectCollection.Count - 1; i >= 0; i--) { Contact targetContact = this.targetContectCollection[i]; if(!filteredContactEnumerable.Contains(targetContact)) { this.targetContectCollection.Remove(targetContact); } } } #endregion #region 타겟 연락처 컬렉션에 없는 연락처 추가하기 - AddContactNotInTargetContactCollection(filteredContactEnumerable) /// <summary> /// 타겟 연락처 컬렉션에 없는 연락처 추가하기 /// </summary> /// <param name="filteredContactEnumerable">필터 연락처 열거 가능형</param> private void AddContactNotInTargetContactCollection(IEnumerable<Contact> filteredContactEnumerable) { foreach(Contact filteredContact in filteredContactEnumerable) { if(!this.targetContectCollection.Contains(filteredContact)) { this.targetContectCollection.Add(filteredContact); } } } #endregion } |