Today
-
Yesterday
-
Total
-

ABOUT ME

-

방문자

  • [WPF Tip] 쿼리 표현식을 이용한 검색(A query expression)
    Microsoft .NET/WPF Tip 2019. 7. 15. 15:49

    입력된 문자가 포함된 단어를 검색하는 프로그램.

    2개의 ObservableCollection과 쿼리 표현식을 이용하여 쉽게 검색 기능 만드는 방법입니다.

     

     


     

    <Window x:Class="WPF_EXAMPLE_aQueryExpression.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" Height="250" Width="300">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            
            <TextBox
                x:Name="tbText"
                Grid.Row="0" Grid.Column="0"
                Text="이"/>
            
            <Button
                x:Name="btnSearch"
                Grid.Row="0" Grid.Column="1"
                Content="검색" Click="BtnSearch_Click"/>
            
            <ListView 
                x:Name="lvResult" 
                Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding}"/> 
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>

     


     

    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows;
    
    namespace WPF_EXAMPLE_aQueryExpression
    {
        /// <summary>
        /// MainWindow.xaml에 대한 상호 작용 논리
        /// </summary>
        public partial class MainWindow : Window
        {
            private ObservableCollection<string> obData = new ObservableCollection<string>() { "당근", "오이", "수박", "토마토", "이이", "이만원", "가지", "오렌지", "지우개", "호박" };
            private ObservableCollection<string> obResult { get; set; }
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void BtnSearch_Click(object sender, RoutedEventArgs e)
            {
                if (tbText.Text.Length > 0)
                {
                    //쿼리 표션식을 이용한 데이터 선택
                    obResult = new ObservableCollection<string>(from s in obData where s.Contains(tbText.Text) select s);
    
                    //ItemsSource와 연결
                    lvResult.ItemsSource = obResult;
                }
            }
        }
    }

     


    별 내용은 없지만 필요하신 분을 위해 샘플 첨부합니다.

     

    WPF_EXAMPLE_aQueryExpression.zip
    0.04MB

Posted by virtualgiraffe.