14. ListView - 2
01.
더보기
1.
Title="MainWindow"
Height="400"
Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="4" />
<RowDefinition Height="auto" />
<RowDefinition Height="4" />
<RowDefinition Height="auto" />
<RowDefinition Height="4" />
<RowDefinition Height="auto" />
<RowDefinition Height="4" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4" />
</Grid.ColumnDefinitions>
<Button Grid.Row="1"
Grid.Column="1"
Content="New Contact"
Click="Button_Click" />
<TextBox Grid.Row="3"
Grid.Column="1"
TextChanged="TextBox_TextChanged"/>
<ListView Grid.Row="5"
Grid.Column="1"
Height="330"
x:Name="contactListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"
FontSize="20"
FontWeight="Bold"/>
<TextBlock Text="{Binding Email}"
FontSize="15"
Foreground="DodgerBlue"/>
<TextBlock Text="{Binding Phone}"
FontStyle="Italic"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
2.
3.
public partial class MainWindow : Window
{
List<Contact> contacts;
public MainWindow()
{
InitializeComponent();
contacts = new List<Contact>();
ReadDataBase();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NewContactWindow newContactWindow = new NewContactWindow();
newContactWindow.ShowDialog();
ReadDataBase();
}
void ReadDataBase()
{
using (SQLiteConnection connection = new SQLiteConnection(App.databasePath))
{
connection.CreateTable<Contact>();
contacts = connection.Table<Contact>().ToList();
}
if (contacts != null)
{
// contact 추가시, 이전 로드된 데이터는 UI에서 사라지도록 구현합니다.
contactListView.ItemsSource = contacts;
}
}
// 텍스트 박스에서 문자가 변경되면
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
// sender를 통해, searchTextBox 객체를 가져와
TextBox? searchTextBox = sender as TextBox;
// contacts 객체 Name 필드 중, searchTextBox.Text 값이 포함된(Contains) 결과를 searchResultList에 담아
var searchResultList = contacts.Where(contact => contact.Name.Contains(searchTextBox.Text)).ToList();
// contactListView 컨트롤에 출력
contactListView.ItemsSource = searchResultList;
}
}
4.
02.
더보기
1.
// contacts 객체 Name 필드 중, searchTextBox.Text 값이 포함된(Contains) 결과를 searchResultList에 담아
// contacts 객체 Name 필드 값을 소문자로 가져옵니다.
// searchTextBox 컨트롤 객체의 text 요소를 소문자로 가져옵니다.
var searchResultList = contacts.Where(contact => contact.Name.ToLower().Contains(searchTextBox.Text.ToLower())).ToList();
2.
03.