■ CollectionView 클래스 : 필터 사용하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    ResizeMode="CanMinimize"

    SizeToContent="WidthAndHeight"

    Title="CollectionView 클래스 : 필터 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <DockPanel Name="dockPanel">

        <Menu DockPanel.Dock="Top">

            <MenuItem Header="_File">

                <MenuItem Header="_Open..." Command="Open" />

            </MenuItem>

        </Menu>

        <Grid>

            <Grid.RowDefinitions>

                <RowDefinition Height="Auto" />

                <RowDefinition Height="Auto" />

            </Grid.RowDefinitions>

            <GroupBox Grid.Row="0"

                Margin="24"

                HorizontalAlignment="Center"

                Header="Criteria">

                <StackPanel>

                    <RadioButton Name="livingRadioButton"

                        Margin="6"

                        Content="Living"

                        Checked="radioButton_Checked" />

                    <RadioButton Name="deadRadioButton"

                        Margin="6"

                        Content="Dead"

                        Checked="radioButton_Checked" />

                    <RadioButton Name="allRadioButton"

                        Margin="6"

                        Content="All"

                        Checked="radioButton_Checked" />

                </StackPanel>

            </GroupBox>

            <ListBox Name="listBox" Grid.Row="1"

                HorizontalAlignment="Center"

                Margin="24"

                Width="300"

                Height="300">

                <ListBox.ItemTemplate>

                    <DataTemplate>

                        <StackPanel Orientation="Horizontal">

                            <TextBlock Text="{Binding Path=FirstName}" />

                            <TextBlock Text=" " />

                            <TextBlock Text="{Binding Path=MiddleName}" />

                            <TextBlock Name="spaceTextBlock" Text=" " />

                            <TextBlock Text="{Binding Path=LastName}" />

                            <TextBlock Text=" (" />

                            <TextBlock Text="{Binding Path=BirthDate.Year}" />

                            <TextBlock Text="-" />

                            <TextBlock Name="deathDateTextBlock" Text="{Binding Path=DeathDate.Year}" />

                            <TextBlock Text=")" />

                        </StackPanel>

                        <DataTemplate.Triggers>

                            <DataTrigger

                                Binding="{Binding Path=DeathDate}"

                                Value="{x:Null}">

                                <Setter TargetName="deathDateTextBlock" Property="Text" Value="present" />

                            </DataTrigger>

                            <DataTrigger Binding="{Binding Path=MiddleName}" Value="">

                                <Setter TargetName="spaceTextBlock" Property="Text" Value="" />

                            </DataTrigger>

                        </DataTemplate.Triggers>

                    </DataTemplate>

                </ListBox.ItemTemplate>

            </ListBox>

        </Grid>

    </DockPanel>

    <Window.CommandBindings>

        <CommandBinding Command="Open" Executed="ExecuteOpen" />

    </Window.CommandBindings>

</Window>

 

 

MainWindow.cs

 

 

using System;

using System.ComponentModel;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 리스트 컬렉션 뷰

        /// </summary>

        private ListCollectionView listCollectionView;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        public static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Open 실행하기 - ExecuteOpen(sender, e)

 

        /// <summary>

        /// Open 실행하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)

        {

            People people = People.Load(this);

 

            if(people != null)

            {

                this.listCollectionView = new ListCollectionView(people);

 

                this.listCollectionView.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending));

 

                this.listBox.ItemsSource = this.listCollectionView;

 

                if(this.listBox.Items.Count > 0)

                {

                    this.listBox.SelectedIndex = 0;

                }

 

                this.allRadioButton.IsChecked = true;

            }

        }

 

        #endregion

        #region 생존자 필터 처리하기 - LivingPersionFilter(value)

 

        /// <summary>

        /// 생존자 필터 처리하기

        /// </summary>

        /// <param name="value"></param>

        /// <returns>처리 결과</returns>

        private bool LivingPersionFilter(object value)

        {

            return (value as Person).DeathDate == null;

        }

 

        #endregion

        #region 사망자 필터 처리하기 - DeadPersonFilter(value)

 

        /// <summary>

        /// 사망자 필터 처리하기

        /// </summary>

        /// <param name="value"></param>

        /// <returns>처리 결과</returns>

        private bool DeadPersonFilter(object value)

        {

            return (value as Person).DeathDate != null;

        }

 

        #endregion

        #region 라디오 버튼 체크 변경시 처리하기 - radioButton_Checked(sender, e)

 

        /// <summary>

        /// 라디오 버튼 체크 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void radioButton_Checked(object sender, RoutedEventArgs e)

        {

            if(this.listCollectionView == null)

            {

                return;

            }

 

            RadioButton radioButton = e.Source as RadioButton;

 

            switch(radioButton.Name)

            {

                case "livingRadioButton" : this.listCollectionView.Filter = LivingPersionFilter; break;

                case "deadRadioButton"   : this.listCollectionView.Filter = DeadPersonFilter;    break;

                case "allRadioButton"    : this.listCollectionView.Filter = null;                break;

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG