■ ObservableCollection<T> 클래스 : 바인딩 설정하기

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


TestProject.zip


Student.cs

 

 

namespace TestProject

{

    /// <summary>

    /// 학생

    /// </summary>

    public class Student

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 학생명 - StudentName

 

        /// <summary>

        /// 학생명

        /// </summary>

        public string StudentName { get; set; }

 

        #endregion

        #region 등록 여부 - IsEnrolled

 

        /// <summary>

        /// 등록 여부

        /// </summary>

        public bool IsEnrolled { get; set; }

 

        #endregion

 

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

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

 

        #region 생성자 - Student()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public Student()

        {

        }

 

        #endregion

    }

}

 

 

StudentCollection.cs

 

 

using System.Collections.ObjectModel;

 

namespace TestProject

{

    /// <summary>

    /// 학생 리스트

    /// </summary>

    public class StudentCollection : ObservableCollection<Student>

    {

    }

}

 

 

BoolToBrushValueConverter.cs

 

 

using System;

using System.Globalization;

using System.Windows.Data;

using System.Windows.Media;

 

namespace TestProject

{

    [ValueConversion(typeof(bool), typeof(Brush))]

    public class BoolToBrushValueConverter : IValueConverter

    {

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

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

 

        #region 변환하기 - Convert(value, targetType, parameter, cultureInfo)

 

        /// <summary>

        /// 변환하기

        /// </summary>

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

        /// <param name="targetType">타겟 타입</param>

        /// <param name="parameter">매개 변수</param>

        /// <param name="cultureInfo">문화 정보</param>

        /// <returns>변환 값</returns>

        public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)

        {

            Brush brush = null;

 

            if(value != null && value.GetType() == typeof(bool))

            {

                brush = (bool)value ? Brushes.Blue : Brushes.Red;

            }

 

            return brush;

        }

 

        #endregion

        #region 역변환하기 - ConvertBack(value, targetType, parameter, cultureInfo)

 

        /// <summary>

        /// 역변환하기

        /// </summary>

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

        /// <param name="targetType">타겟 타입</param>

        /// <param name="parameter">매개 변수</param>

        /// <param name="cultureInfo">문화 정보</param>

        /// <returns>역변환 값</returns>

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo)

        {

            return null;

        }

 

        #endregion

    }

}

 

 

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"

    xmlns:local="clr-namespace:TestProject"

    Width="600"

    Height="450"

    Title="ObservableCollection&lt;T&gt; 클래스 : 바인딩 설정하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Window.Resources>

        <local:StudentCollection x:Key="StudentCollectionKey">

            <local:Student StudentName="홍길동" IsEnrolled="false" />

            <local:Student StudentName="김철수" IsEnrolled="true"  />

            <local:Student StudentName="이영희" IsEnrolled="false" />

            <local:Student StudentName="정순자" IsEnrolled="true"  />

            <local:Student StudentName="방영수" IsEnrolled="true"  />

        </local:StudentCollection>

        <local:BoolToBrushValueConverter x:Key="BoolToBrushValueConverterKey" />

        <DataTemplate x:Key="ListBoxItemDataTemplateKey">

            <StackPanel

                Margin="10"

                Orientation="Horizontal">

                <Rectangle

                    Margin="0 0 5 0"

                    Height="10"

                    Width="10"

                    Fill="{Binding Path=IsEnrolled, Converter={StaticResource BoolToBrushValueConverterKey}}" />

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

            </StackPanel>

        </DataTemplate>

    </Window.Resources>

    <Grid>

        <ListBox

            Margin="10 10 10 10"

            ItemsSource="{StaticResource StudentCollectionKey}"

            ItemTemplate="{StaticResource ListBoxItemDataTemplateKey}" />

    </Grid>

</Window>

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요