첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

TestProject.zip
다운로드

▶ Course.cs

namespace TestProject
{
    /// <summary>
    /// 코스
    /// </summary>
    public class Course
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 제목 - Title

        /// <summary>
        /// 제목
        /// </summary>
        public string Title { get; set; }

        #endregion
        #region 섹션 ID - SectionID

        /// <summary>
        /// 섹션 ID
        /// </summary>
        public int SectionID { get; set; }

        #endregion
        #region 설명 - Description

        /// <summary>
        /// 설명
        /// </summary>
        public string Description { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Course()

        /// <summary>
        /// 생성자
        /// </summary>
        public Course()
        {
        }

        #endregion
        #region 생성자 - Course(title, sectionID, description)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="title">제목</param>
        /// <param name="sectionID">섹션 ID</param>
        /// <param name="description">설명</param>
        public Course(string title, int sectionID, string description)
        {
            Title       = title;
            SectionID   = sectionID;
            Description = description;
        }

        #endregion
    }
}

 

▶ Student.cs

using System;
using System.Collections.ObjectModel;
using System.Text;

namespace TestProject
{
    /// <summary>
    /// 학생
    /// </summary>
    public class Student : IFormattable
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 성명 - Name

        /// <summary>
        /// 성명
        /// </summary>
        public string Name { get; set; }

        #endregion
        #region 코스 컬렉션 - CourseCollection

        /// <summary>
        /// 코스 컬렉션
        /// </summary>
        ObservableCollection<Course> CourseCollection { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Student(name)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="name">성명</param>
        public Student(string name)
        {
            Name             = name;
            CourseCollection = new ObservableCollection<Course>();
        }

        #endregion
        #region 생성자 - Student()

        /// <summary>
        /// 생성자
        /// </summary>
        public Student() : this(string.Empty)
        {
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 코스 추가하기 - AddCourse(name, sectionID, description)

        /// <summary>
        /// 코스 추가하기
        /// </summary>
        /// <param name="name">성명</param>
        /// <param name="sectionID">섹션 ID</param>
        /// <param name="description">설명</param>
        public void AddCourse(string name, int sectionID, string description)
        {
            CourseCollection.Add(new Course(name, sectionID, description));
        }

        #endregion
        #region 문자열 구하기 - ToString(format, formatProvider)

        /// <summary>
        /// 문자열 구하기
        /// </summary>
        /// <param name="format">포맷 문자열</param>
        /// <param name="formatProvider">포맷 제공자</param>
        /// <returns>문자열</returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if(format == "Name")
            {
                return Name;
            }

            if(format == "CourseList")
            {
                string stringFormat = "{0,-25}{1,-30}{2,-10}\r\n";

                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.AppendLine(); 
                stringBuilder.AppendFormat(stringFormat, "Title", "Description", "ID");
                stringBuilder.AppendLine(); 

                foreach(Course course in CourseCollection)
                {
                    stringBuilder.AppendFormat(stringFormat, course.Title, course.Description, course.SectionID);
                }
        
                return stringBuilder.ToString();
            }

            return this.ToString();
        }

        #endregion
    }
}

 

▶ StudentCollection.cs

using System.Collections.ObjectModel;

namespace TestProject
{
    /// <summary>
    /// 학생 컬렉션
    /// </summary>
    public class StudentCollection : ObservableCollection<Student>
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - StudentCollection()

        /// <summary>
        /// 생성자
        /// </summary>
        public StudentCollection()
        {
            Student student1 = new Student("Sunil Uppal");

            student1.AddCourse("Calculus 303"          , 19, "Advanced Calculus"            );
            student1.AddCourse("History 110"           , 35, "Introduction to World History");
            student1.AddCourse("Psychology 110"        , 40, "Behavioral Psychology"        );
            student1.AddCourse("Physical Education 204", 80, "Racquetball"                  );

            Add(student1);

            Student student2 = new Student("Alice Ciccu");

            student2.AddCourse("English 200", 50 , "Advanced Composition"      );
            student2.AddCourse("English 315", 100, "Shakespear's Sonnets"      );
            student2.AddCourse("History 230", 38 , "European History 1000-1500");

            Add(student2);

            Student student3 = new Student("Jeff Price");

            student3.AddCourse("History 230"           , 38, "European History 1000-1500"   );
            student3.AddCourse("History 110"           , 35, "Introduction to World History");
            student3.AddCourse("Physical Education 204", 80, "Racquetball"                  );

            Add(student3);
        }

        #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="800"
    Height="600"
    Title="TabControl 엘리먼트 : ContentStringFormat 속성을 사용해 컨텐트 포맷 문자열 설정하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <local:StudentCollection x:Key="StudentCollectionKey" />
        <Style TargetType="TabItem">
            <Setter Property="HeaderStringFormat" Value="Name"  />
            <Setter Property="Padding"            Value="5"     />
            <Setter Property="Foreground"         Value="Green" />
            <Setter Property="FontWeight"         Value="Bold"  />
        </Style>
    </Window.Resources>
    <Grid>
        <TabControl
            Margin="10"
            Padding="10"
            Foreground="Navy"
            ContentStringFormat="CourseList"
            ItemsSource="{StaticResource StudentCollectionKey}" />
    </Grid>
</Window>
728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요