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

■ 폰트 대화 상자 사용하기

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


TestProject.zip


UIHelper.cs

 

 

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Linq;

using System.Windows;

using System.Windows.Markup;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// UI 헬퍼

    /// </summary>

    public class UIHelper

    {

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

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

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

 

        #region 의존 속성 등록하기 - RegisterDependencyProperty(defaultValue, affectsRender, affectsMeasure, affectsArrange,

            inherits, propertyChangedCallback, propertyName, propertyType, ownerType, validateValueCallback)

 

        /// <summary>

        /// 의존 속성 등록하기

        /// </summary>

        /// <param name="defaultValue">디폴트 값</param>

        /// <param name="affectsRender">렌더링 여향 여부</param>

        /// <param name="affectsMeasure">크기 측정 영향 여부</param>

        /// <param name="affectsArrange">배치 영향 여부</param>

        /// <param name="inherits">상속 여부</param>

        /// <param name="propertyChangedCallback">속성 변경 콜백</param>

        /// <param name="propertyName">속성명</param>

        /// <param name="propertyType">속성 타입</param>

        /// <param name="ownerType">소유자 타입</param>

        /// <param name="validateValueCallback">값 무결성 콜백</param>

        /// <returns>의존 속성</returns>

        public static DependencyProperty RegisterDependencyProperty(object defaultValue, bool affectsRender,

            bool affectsMeasure, bool affectsArrange, bool inherits, PropertyChangedCallback propertyChangedCallback,

            string propertyName, Type propertyType, Type ownerType, ValidateValueCallback validateValueCallback)

        {

            FrameworkPropertyMetadata frameworkPropertyMetadata = new FrameworkPropertyMetadata();

 

            frameworkPropertyMetadata.DefaultValue   = defaultValue;

            frameworkPropertyMetadata.AffectsRender  = affectsRender;

            frameworkPropertyMetadata.AffectsMeasure = affectsMeasure;

            frameworkPropertyMetadata.AffectsArrange = affectsArrange;

            frameworkPropertyMetadata.Inherits       = inherits;

 

            frameworkPropertyMetadata.PropertyChangedCallback += propertyChangedCallback;

 

            return DependencyProperty.Register(propertyName, propertyType, ownerType, frameworkPropertyMetadata, validateValueCallback);

        }

 

        #endregion

 

        #region 폰트명 구하기 - GetFontName(fontFamily)

 

        /// <summary>

        /// 폰트명 구하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

        /// <returns>폰트명</returns>

        public static string GetFontName(FontFamily fontFamily)

        {

            if(fontFamily == null)

            {

                return string.Empty;

            }

 

            if(fontFamily.FamilyNames.Count > 1 &&

                fontFamily.FamilyNames.Keys.ElementAt<XmlLanguage>(fontFamily.FamilyNames.Count - 1).IetfLanguageTag == "ko-kr")

            {

                return fontFamily.FamilyNames.Values.ElementAt<string>(fontFamily.FamilyNames.Count - 1);

            }

            else

            {

                return fontFamily.FamilyNames.Values.ElementAt<string>(0);

            }

        }

 

        #endregion

 

        #region 폰트 패밀리 데이터 구하기 - GetFontFamilyData()

 

        /// <summary>

        /// 폰트 패밀리 데이터 구하기

        /// </summary>

        /// <returns>폰트 패밀리 데이터</returns>

        public static ObservableCollection<ListItem> GetFontFamilyData()

        {

            SortedList<string, FontFamily> sortedList = new SortedList<string, FontFamily>();

 

            foreach(FontFamily fontFamily in Fonts.SystemFontFamilies)

            {

                string fontName = UIHelper.GetFontName(fontFamily);

 

                sortedList.Add(fontName, fontFamily);

            }

 

            ObservableCollection<ListItem> collection = new ObservableCollection<ListItem>();

 

            for(int i = 0; i < sortedList.Keys.Count; i++)

            {

                ListItem item = new ListItem(sortedList.Keys[i]);

 

                item.Tag = sortedList[sortedList.Keys[i]];

 

                collection.Add(item);

            }

 

            return collection;

        }

 

        #endregion

        #region 폰트 스타일 데이터 구하기 - GetFontStyleData(fontFamily)

 

        /// <summary>

        /// 폰트 스타일 데이터 구하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

        /// <returns>폰트 스타일 데이터</returns>

        public static ObservableCollection<ListItem> GetFontStyleData(FontFamily fontFamily)

        {

            List<FontStyle> list = new List<FontStyle>();

 

            foreach(FamilyTypeface familyTypeface in fontFamily.FamilyTypefaces)

            {

                if(!list.Contains(familyTypeface.Style))

                {

                    if(familyTypeface.Style == FontStyles.Normal)

                    {

                        list.Insert(0, familyTypeface.Style);

                    }

                    else

                    {

                        list.Add(familyTypeface.Style);

                    }

                }

            }

 

            ObservableCollection<ListItem> collection = new ObservableCollection<ListItem>();

 

            foreach(FontStyle fontStyle in list)

            {

                ListItem item = new ListItem(fontStyle.ToString());

 

                item.Tag = fontStyle;

 

                collection.Add(item);

            }

 

            return collection;

        }

 

        #endregion

        #region 폰트 웨이트 데이터 구하기 - GetFontWeightData(fontFamily)

 

        /// <summary>

        /// 폰트 웨이트 데이터 구하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

        /// <returns>폰트 웨이트 데이터</returns>

        public static ObservableCollection<ListItem> GetFontWeightData(FontFamily fontFamily)

        {

            List<FontWeight> list = new List<FontWeight>();

 

            foreach(FamilyTypeface familyTypeface in fontFamily.FamilyTypefaces)

            {

                if(!list.Contains(familyTypeface.Weight))

                {

                    if(familyTypeface.Weight == FontWeights.Normal)

                    {

                        list.Insert(0, familyTypeface.Weight);

                    }

                    else

                    {

                        list.Add(familyTypeface.Weight);

                    }

                }

            }

 

            ObservableCollection<ListItem> collection = new ObservableCollection<ListItem>();

 

            foreach(FontWeight fontWeight in list)

            {

                ListItem item = new ListItem(fontWeight.ToString());

 

                item.Tag = fontWeight;

 

                collection.Add(item);

            }

 

            return collection;

        }

 

        #endregion

        #region 폰트 스트레치 데이터 구하기 - GetFontStretchData(fontFamily)

 

        /// <summary>

        /// 폰트 스트레치 데이터 구하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

        /// <returns>폰트 스트레치 데이터</returns>

        public static ObservableCollection<ListItem> GetFontStretchData(FontFamily fontFamily)

        {

            List<FontStretch> list = new List<FontStretch>();

 

            foreach(FamilyTypeface familyTypeface in fontFamily.FamilyTypefaces)

            {

                if(!list.Contains(familyTypeface.Stretch))

                {

                    if(familyTypeface.Stretch == FontStretches.Normal)

                    {

                        list.Insert(0, familyTypeface.Stretch);

                    }

                    else

                    {

                        list.Add(familyTypeface.Stretch);

                    }

                }

            }

 

            ObservableCollection<ListItem> collection = new ObservableCollection<ListItem>();

 

            foreach(FontStretch fontStretch in list)

            {

                ListItem item = new ListItem(fontStretch.ToString());

 

                item.Tag = fontStretch;

 

                collection.Add(item);

            }

 

            return collection;

        }

 

        #endregion

        #region 폰트 크기 데이터 구하기 - GetFontSizeData()

 

        /// <summary>

        /// 폰트 크기 데이터 구하기

        /// </summary>

        /// <returns>폰트 크기 데이터</returns>

        public static ObservableCollection<ListItem> GetFontSizeData()

        {

            double[] fontSizeArray = new double[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };

 

            ObservableCollection<ListItem> collection = new ObservableCollection<ListItem>();

 

            foreach(double fontSize in fontSizeArray)

            {

                ListItem item = new ListItem(fontSize.ToString());

 

                item.Tag = fontSize;

 

                collection.Add(item);

            }

 

            return collection;

        }

 

        #endregion

    }

}

 

 

ListItem.cs

 

 

using System.ComponentModel;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 리스트 항목

    /// </summary>

    public class ListItem : INotifyPropertyChanged

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Event

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

 

        #region 속성 변경시 - PropertyChanged

 

        /// <summary>

        /// 속성 변경시

        /// </summary>

        public event PropertyChangedEventHandler PropertyChanged;

 

        #endregion

 

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

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

 

        #region Field

 

        /// <summary>

        /// 체크 여부

        /// </summary>

        private bool isChecked;

 

        /// <summary>

        /// 아이콘

        /// </summary>

        private ImageSource icon;

 

        /// <summary>

        ///

        /// </summary>

        private string value;

 

        /// <summary>

        /// 텍스트

        /// </summary>

        private string text;

 

        /// <summary>

        /// 태그

        /// </summary>

        private object tag;

 

        #endregion

 

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

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

 

        #region 체크 여부 - IsChecked

 

        /// <summary>

        /// 체크 여부

        /// </summary>

        public bool IsChecked

        {

            get

            {

                return this.isChecked;

            }

            set

            {

                if(this.isChecked == value)

                {

                    return;

                }

 

                this.isChecked = value;

 

                OnPropertyChanged("IsChecked");

            }

        }

 

        #endregion

        #region 아이콘 - Icon

 

        /// <summary>

        /// 아이콘

        /// </summary>

        public ImageSource Icon

        {

            get

            {

                return this.icon;

            }

            set

            {

                if(this.icon == value)

                {

                    return;

                }

 

                this.icon = value;

 

                OnPropertyChanged("Icon");

            }

        }

 

        #endregion

        #region 값 - Value

 

        /// <summary>

        ///

        /// </summary>

        public string Value

        {

            get

            {

                return this.value;

            }

            set

            {

                if(this.value == value)

                {

                    return;

                }

 

                this.value = value;

 

                OnPropertyChanged("Value");

            }

        }

 

        #endregion

        #region 텍스트 - Text

 

        /// <summary>

        /// 텍스트

        /// </summary>

        public string Text

        {

            get

            {

                return this.text;

            }

            set

            {

                if(this.text == value)

                {

                    return;

                }

 

                this.text = value;

 

                OnPropertyChanged("Text");

            }

        }

 

        #endregion

        #region 태그 - Tag

 

        /// <summary>

        /// 태그

        /// </summary>

        public object Tag

        {

            get

            {

                return this.tag;

            }

            set

            {

                if(this.tag == value)

                {

                    return;

                }

 

                this.tag = value;

 

                OnPropertyChanged("Tag");

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - MultiItem()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public ListItem()

        {

            this.isChecked = false;

            this.icon      = null;

            this.value     = null;

            this.text      = null;

            this.tag       = null;

        }

 

        #endregion

        #region 생성자 - MultiItem(value)

 

        /// <summary>

        /// 생성자

        /// </summary>

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

        public ListItem(string value)

        {

            this.isChecked = false;

            this.icon      = null;

            this.value     = value;

            this.text      = value;

            this.tag       = null;

        }

 

        #endregion

        #region 생성자 - MultiItem(value, text)

 

        /// <summary>

        /// 생성자

        /// </summary>

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

        /// <param name="text">텍스트</param>

        public ListItem(string value, string text)

        {

            this.isChecked = false;

            this.icon      = null;

            this.value     = value;

            this.text      = text;

            this.tag       = null;

        }

 

        #endregion

        #region 생성자 - MultiItem(isChecked, value, text)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="isChecked">체크 여부</param>

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

        /// <param name="text">텍스트</param>

        public ListItem(bool isChecked, string value, string text)

        {

            this.isChecked = isChecked;

            this.icon      = null;

            this.value     = value;

            this.text      = text;

            this.tag       = null;

        }

 

        #endregion

        #region 생성자 - MultiItem(isChecked, icon, value, text)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="isChecked">체크 여부</param>

        /// <param name="icon">아이콘</param>

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

        /// <param name="text">텍스트</param>

        public ListItem(bool isChecked, ImageSource icon, string value, string text)

        {

            this.isChecked = isChecked;

            this.icon      = icon;

            this.value     = value;

            this.text      = text;

            this.tag       = null;

        }

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 속성 변경시 처리하기 - OnPropertyChanged(proertyName)

 

        /// <summary>

        /// 속성 변경시 처리하기

        /// </summary>

        /// <param name="proertyName">속성명</param>

        protected void OnPropertyChanged(string proertyName)

        {

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(proertyName));

        }

 

        #endregion

    }

}

 

 

SingleListBox.xaml

 

 

<ListBox x:Class="TestProject.SingleListBox"

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

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

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    mc:Ignorable="d"

    d:DesignWidth="200"

    d:DesignHeight="200"

    ScrollViewer.HorizontalScrollBarVisibility="Hidden"

    SelectionMode="Single">

    <ListBox.ItemTemplate>

        <DataTemplate>

            <StackPanel

                Margin="1"

                Orientation="Horizontal">

                <Image Name="itemImage"

                    VerticalAlignment="Center"

                    Margin="0 0 5 0"

                    Width="16"

                    Height="16"

                    Stretch="None"

                    Source="{Binding Icon}" />

                <TextBlock Name="itemTextBlock"

                    VerticalAlignment="Center"

                    Margin="0"

                    Width="{Binding RelativeSource={RelativeSource AncestorType=ScrollContentPresenter, Mode=FindAncestor}, Path=ActualWidth}"

                    Text="{Binding Text}" />

            </StackPanel>

            <DataTemplate.Triggers>

                <DataTrigger Binding="{Binding Icon}" Value="{x:Null}">

                    <Setter TargetName="itemImage" Property="Visibility" Value="Collapsed" />

                </DataTrigger>

            </DataTemplate.Triggers>

        </DataTemplate>

    </ListBox.ItemTemplate>

</ListBox>

 

 

SingleListBox.xaml.cs

 

 

using System;

using System.Collections.ObjectModel;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 싱글 리스트 박스

    /// </summary>

    public partial class SingleListBox : ListBox

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Event

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

 

        #region 값 변경시 - ValueChanged

 

        /// <summary>

        /// 값 변경시

        /// </summary>

        public event EventHandler ValueChanged;

 

        #endregion

        #region 항목 마우스 UP시 - ItemMouseUp

 

        /// <summary>

        /// 항목 마우스 UP시

        /// </summary>

        public event EventHandler ItemMouseUp;

 

        #endregion

 

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

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

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

 

        #region Field

 

        /// <summary>

        /// 값 의존 속성

        /// </summary>

        public static readonly DependencyProperty ValueProperty;

 

        /// <summary>

        /// 텍스트 의존 속성

        /// </summary>

        public static readonly DependencyProperty TextProperty;

 

        #endregion

 

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

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

 

        #region Field

 

        /// <summary>

        /// 값 변경시 이벤트 발생 가능 여부

        /// </summary>

        private bool canFireValueChanged = true;

 

        #endregion

 

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

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

 

        #region 값 - Value

 

        /// <summary>

        ///

        /// </summary>

        public string Value

        {

            get

            {

                return (string)GetValue(ValueProperty);

            }

            set

            {

                SetValue(ValueProperty, value);

            }

        }

 

        #endregion

        #region 텍스트 - Text

 

        /// <summary>

        /// 텍스트

        /// </summary>

        public string Text

        {

            get

            {

                return (string)GetValue(TextProperty);

            }

            set

            {

                SetValue(TextProperty, value);

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - SingleListBox()

 

        /// <summary>

        /// 생성자

        /// </summary>

        static SingleListBox()

        {

            ValueProperty = UIHelper.RegisterDependencyProperty(string.Empty, false, false, false, true, valuePropertyChangedCallback,

                "Value", typeof(string), typeof(SingleListBox), null);

            TextProperty = UIHelper.RegisterDependencyProperty(string.Empty, false, false, false, true, null,

                "Text", typeof(string), typeof(SingleListBox), null);

        }

 

        #endregion

 

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

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

 

        #region 생성자 - SingleListBox()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public SingleListBox()

        {

            InitializeComponent();

 

            SelectionChanged += listBox_SelectionChanged;

 

            AddHandler(TextBlock.MouseDownEvent, new MouseButtonEventHandler(itemTextBlock_MouseDown));

            AddHandler(TextBlock.MouseUpEvent  , new MouseButtonEventHandler(itemTextBlock_MouseUp  ));

        }

 

        #endregion

 

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

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

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

 

        #region 값 속성 변경시 콜백 처리하기 - valuePropertyChangedCallback(dependencyObject, e)

 

        /// <summary>

        /// 값 속성 변경시 콜백 처리하기

        /// </summary>

        /// <param name="dependencyObject">의존 객체</param>

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

        private static void valuePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)

        {

            SingleListBox singleListBox = dependencyObject as SingleListBox;

 

 

            string newValue = e.NewValue == null ? string.Empty : e.NewValue as string;

 

            singleListBox.SetValue(newValue);

 

 

            string text = singleListBox.GetText();

 

            singleListBox.Text = text;

 

 

            singleListBox.FireValueChanged();

        }

 

        #endregion

 

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

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

 

        #region 항목 구하기 - GetItem()

 

        /// <summary>

        /// 항목 구하기

        /// </summary>

        /// <returns>항목</returns>

        public ListItem GetItem()

        {

            ListItem item = SelectedItem as ListItem;

 

            return item;

        }

 

        #endregion

        #region 항목 구하기 - GetItem(index)

 

        /// <summary>

        /// 항목 구하기

        /// </summary>

        /// <param name="index">인덱스</param>

        /// <returns>항목</returns>

        public ListItem GetItem(int index)

        {

            ObservableCollection<ListItem> collection = ItemsSource as ObservableCollection<ListItem>;

 

            if(collection == null || collection.Count == 0)

            {

                return null;

            }

 

            if(index < 0 || index > collection.Count - 1)

            {

                return null;

            }

 

            ListItem item = collection[index];

 

            return item;

        }

 

        #endregion

        #region 값 포함 여부 구하기 - Contains(value)

 

        /// <summary>

        /// 값 포함 여부 구하기

        /// </summary>

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

        /// <returns>값 포함 여부</returns>

        public bool Contains(string value)

        {

            ObservableCollection<ListItem> collection = ItemsSource as ObservableCollection<ListItem>;

 

            if(collection == null || collection.Count == 0)

            {

                return false;

            }

 

            foreach(ListItem item in collection)

            {

                if(item.Value.Equals(value))

                {

                    return true;

                }

            }

 

            return false;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 값 변경시 이벤트 발생시키기 - FireValueChanged()

 

        /// <summary>

        /// 값 변경시 이벤트 발생시키기

        /// </summary>

        protected void FireValueChanged()

        {

            if(ValueChanged != null)

            {

                ValueChanged(this, EventArgs.Empty);

            }

        }

 

        #endregion

        #region 항목 마우스 UP시 이벤트 발생시키기 - FireItemMouseUp()

 

        /// <summary>

        /// 항목 마우스 UP시 이벤트 발생시키기

        /// </summary>

        protected void FireItemMouseUp()

        {

            if(ItemMouseUp != null)

            {

                ItemMouseUp(this, EventArgs.Empty);

            }

        }

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////// Event

 

        #region 리스트 박스 선택 변경시 처리하기 - listBox_SelectionChanged(sender, e)

 

        /// <summary>

        /// 리스트 박스 선택 변경시 처리하기

        /// </summary>

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

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

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            if(this.canFireValueChanged)

            {

                ListItem item = SelectedItem as ListItem;

 

                if(item == null)

                {

                    Value = string.Empty;

 

                    return;

                }

 

                Value = item.Value;

            }

        }

 

        #endregion

        #region 항목 텍스트 블럭 마우스 DOWN 처리하기 - itemTextBlock_MouseDown(sender, e)

 

        /// <summary>

        /// 항목 텍스트 블럭 마우스 DOWN 처리하기

        /// </summary>

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

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

        private void itemTextBlock_MouseDown(object sender, MouseButtonEventArgs e)

        {

            this.canFireValueChanged = false;

        }

 

        #endregion

        #region 항목 텍스트 블럭 마우스 UP 처리하기 - itemTextBlock_MouseUp(sender, e)

 

        /// <summary>

        /// 항목 텍스트 블럭 마우스 UP 처리하기

        /// </summary>

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

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

        private void itemTextBlock_MouseUp(object sender, MouseButtonEventArgs e)

        {

            this.canFireValueChanged = true;

 

            Value = GetValue();

 

            FireItemMouseUp();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////// Function

 

        #region 값 설정하기 - SetValue(value)

 

        /// <summary>

        /// 값 설정하기

        /// </summary>

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

        private void SetValue(string value)

        {

            try

            {

                SelectionChanged -= listBox_SelectionChanged;

 

                ObservableCollection<ListItem> collection = ItemsSource as ObservableCollection<ListItem>;

 

                if(collection == null || collection.Count == 0)

                {

                    return;

                }

 

                if(string.IsNullOrEmpty(value))

                {

                    SelectedItem = null;

 

                    return;

                }

 

                foreach(ListItem item in collection)

                {

                    if(value.Equals(item.Value))

                    {

                        SelectedItem = item;

 

                        return;

                    }

                }

 

                SelectedItem = null;

            }

            finally

            {

                SelectionChanged += listBox_SelectionChanged;

            }

        }

 

        #endregion

        #region 값 구하기 - GetValue()

 

        /// <summary>

        /// 값 구하기

        /// </summary>

        /// <returns></returns>

        private string GetValue()

        {

            if(SelectedItem == null)

            {

                return string.Empty;

            }

 

            ListItem item = SelectedItem as ListItem;

 

            if(item == null)

            {

                return string.Empty;

            }

            else

            {

                return item.Value;

            }

        }

 

        #endregion

        #region 텍스트 구하기 - GetText()

 

        /// <summary>

        /// 텍스트 구하기

        /// </summary>

        /// <returns>텍스트</returns>

        private string GetText()

        {

            if(SelectedItem == null)

            {

                return string.Empty;

            }

 

            ListItem item = SelectedItem as ListItem;

 

            if(item == null)

            {

                return string.Empty;

            }

            else

            {

                return item.Text;

            }

        }

 

        #endregion

    }

}

 

 

FontDialog.xaml

 

 

<Window x:Class="TestProject.FontDialog"

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

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

    xmlns:local="clr-namespace:TestProject"

    ShowInTaskbar="False"

    WindowStyle="ToolWindow"

    WindowStartupLocation="CenterOwner"

    SizeToContent="WidthAndHeight"

    ResizeMode="NoResize"

    Title="Font">

    <Grid Name="layoutGrid" Margin="10">

        <Grid.RowDefinitions>

            <RowDefinition Height="200" />

            <RowDefinition Height="10"   />

            <RowDefinition Height="150" />

            <RowDefinition Height="10"   />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="650" />

        </Grid.ColumnDefinitions>

        <Grid Name="controlGrid" Grid.Row="0" Grid.Column="0">

            <Grid.RowDefinitions>

                <RowDefinition Height="Auto" />

                <RowDefinition Height="Auto" />

                <RowDefinition Height="2"    />

                <RowDefinition Height="100*" />

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="150*" />

                <ColumnDefinition Width="10"   />

                <ColumnDefinition Width="100*" />

                <ColumnDefinition Width="10"   />

                <ColumnDefinition Width="100*" />

                <ColumnDefinition Width="10"   />

                <ColumnDefinition Width="100*" />

                <ColumnDefinition Width="10"   />

                <ColumnDefinition Width="75*" />

            </Grid.ColumnDefinitions>

            <Label Grid.Row="0" Grid.Column="0" Margin="0" Content="Font"    />

            <Label Grid.Row="0" Grid.Column="2" Margin="0" Content="Style"   />

            <Label Grid.Row="0" Grid.Column="4" Margin="0" Content="Weight"  />

            <Label Grid.Row="0" Grid.Column="6" Margin="0" Content="Stretch" />

            <Label Grid.Row="0" Grid.Column="8" Margin="0" Content="Size"    />

 

            <TextBox Grid.Row="1" Grid.Column="0"

                IsReadOnly="True"

                Text="{Binding ElementName=fontFamilySingleListBox, Path=SelectedItem.Text}" />

            <TextBox Grid.Row="1" Grid.Column="2"

                IsReadOnly="True"

                Text="{Binding ElementName=fontStyleSingleListBox, Path=SelectedItem.Text}" />

            <TextBox Grid.Row="1" Grid.Column="4"

                IsReadOnly="True"

                Text="{Binding ElementName=fontWeightSingleListBox, Path=SelectedItem.Text}" />

            <TextBox Grid.Row="1" Grid.Column="6"

                IsReadOnly="True"

                Text="{Binding ElementName=fontStretchSingleListBox, Path=SelectedItem.Text}" />

            <TextBox Grid.Row="1" Grid.Column="8"

                IsReadOnly="True"

                Text="{Binding ElementName=fontSizeSingleListBox, Path=SelectedItem.Text}" />

 

            <local:SingleListBox x:Name="fontFamilySingleListBox" Grid.Row="3" Grid.Column="0" Margin="0" />

            <local:SingleListBox x:Name="fontStyleSingleListBox"   Grid.Row="3" Grid.Column="2" Margin="0" />

            <local:SingleListBox x:Name="fontWeightSingleListBox" Grid.Row="3" Grid.Column="4" Margin="0" />

            <local:SingleListBox x:Name="fontStretchSingleListBox" Grid.Row="3" Grid.Column="6" Margin="0" />

            <local:SingleListBox x:Name="fontSizeSingleListBox"    Grid.Row="3" Grid.Column="8" Margin="0" />

        </Grid>

        <Border Name="displayBorder"

            Grid.Row="2"

            Grid.Column="0"

            BorderBrush="Blue"

            BorderThickness="1">

            <Label Name="displayLabel"

                HorizontalContentAlignment="Center"

                VerticalContentAlignment="Center"

                Content="가나다라마 ABC abc 12345">

                <Label.Foreground>

                    <LinearGradientBrush x:Name="displayBrush">

                        <LinearGradientBrush.GradientStops>

                            <GradientStop Color="Red"    />

                            <GradientStop Color="Orange" />

                            <GradientStop Color="Yellow" />

                            <GradientStop Color="Green" />

                            <GradientStop Color="Blue"   />

                            <GradientStop Color="Indigo" />

                            <GradientStop Color="Violet" />

                        </LinearGradientBrush.GradientStops>

                    </LinearGradientBrush>

                </Label.Foreground>

                <Label.Triggers>

                    <EventTrigger RoutedEvent="Label.Loaded">

                        <BeginStoryboard>

                            <Storyboard

                                TargetName="displayBrush"

                                SpeedRatio="3">

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[0].Offset"

                                    RepeatBehavior="Forever"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[1].Offset"

                                    RepeatBehavior="Forever"

                                    BeginTime="0:0:1"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[2].Offset"

                                    RepeatBehavior="Forever"

                                    BeginTime="0:0:2"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[3].Offset"

                                    RepeatBehavior="Forever"

                                    BeginTime="0:0:3"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[4].Offset"

                                    RepeatBehavior="Forever"

                                    BeginTime="0:0:4"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[5].Offset"

                                    RepeatBehavior="Forever"

                                    BeginTime="0:0:5"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                                <DoubleAnimation

                                    Storyboard.TargetProperty="GradientStops[6].Offset"

                                    RepeatBehavior="Forever"

                                    BeginTime="0:0:6"

                                    Duration="0:0:7"

                                    From="0"

                                    To="1" />

                            </Storyboard>

                        </BeginStoryboard>

                    </EventTrigger>

                </Label.Triggers>

                

            </Label>

        </Border>

        <Grid Name="buttonGrid" Grid.Row="4" Grid.Column="0">

            <Grid.ColumnDefinitions>

                <ColumnDefinition />

                <ColumnDefinition />

                <ColumnDefinition />

                <ColumnDefinition />

                <ColumnDefinition />

            </Grid.ColumnDefinitions>

            <Button Name="okButton"

                Grid.Row="0"

                Grid.Column="1"

                HorizontalAlignment="Center"

                MinWidth="75"

                Padding="2"

                IsDefault="True"

                Content="OK" />

            <Button Name="cancelButton"

                Grid.Row="0"

                Grid.Column="3"

                HorizontalAlignment="Center"

                MinWidth="75"

                Padding="2"

                IsCancel="True"

                Content="Cancel" />

        </Grid>

    </Grid>

</Window>

 

 

FontDialog.xaml.cs

 

 

using System;

using System.Collections.ObjectModel;

using System.Windows;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 폰트 다이얼로그

    /// </summary>

    public partial class FontDialog : Window

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 업데이트 보류 여부

        /// </summary>

        private bool isUpdateSuppressed = true;

 

        #endregion

 

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

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

 

        #region 타입 페이스 - Typeface

 

        /// <summary>

        /// 타입 페이스

        /// </summary>

        public Typeface Typeface

        {

            get

            {

                ListItem fontFamilyListItem = this.fontFamilySingleListBox.GetItem();

 

                if(fontFamilyListItem == null)

                {

                    return null;

                }

 

                ListItem fontStyleListItem = this.fontStyleSingleListBox.GetItem();

 

                if(fontStyleListItem == null)

                {

                    return null;

                }

 

                ListItem fontWeightListItem = this.fontWeightSingleListBox.GetItem();

 

                if(fontWeightListItem == null)

                {

                    return null;

                }

 

                ListItem fontStretchListItem = this.fontStretchSingleListBox.GetItem();

 

                if(fontStretchListItem == null)

                {

                    return null;

                }

 

                Typeface typeface = new Typeface

                (

                    fontFamilyListItem.Tag as FontFamily,

                    (FontStyle)fontStyleListItem.Tag,

                    (FontWeight)fontWeightListItem.Tag,

                    (FontStretch)fontStretchListItem.Tag

                );

 

                return typeface;

            }

            set

            {

                SetSingleListBoxValue(this.fontFamilySingleListBox, UIHelper.GetFontName(value.FontFamily));

 

                this.isUpdateSuppressed = true;

 

                SetSingleListBoxValue(this.fontStyleSingleListBox  , value.Style.ToString()  );

                SetSingleListBoxValue(this.fontWeightSingleListBox , value.Weight.ToString() );

                SetSingleListBoxValue(this.fontStretchSingleListBox, value.Stretch.ToString());

                

                this.isUpdateSuppressed = false;

 

                UpdateSample();

            }

        }

 

        #endregion

        #region 폰트 크기 - FaceSize

 

        /// <summary>

        /// 폰트 크기

        /// </summary>

        /// <remarks>상속받은 Window 클래스에 FontSize 속성이 있기 때문이다.</remarks>

        public double FaceSize

        {

            get

            {

                ListItem item = this.fontSizeSingleListBox.GetItem();

 

                double size;

 

                if(item == null)

                {

                    size = 8.25d;

                }

                else

                {

                    size = (double)item.Tag;

                }

 

                return size / 0.75d;

            }

            set

            {

                double size = 0.75d * value;

 

                string sizeString = size.ToString();

 

                if(!this.fontSizeSingleListBox.Contains(sizeString))

                {

                    ObservableCollection<ListItem> collection = this.fontSizeSingleListBox.ItemsSource as ObservableCollection<ListItem>;

 

                    if(collection == null)

                    {

                        collection = new ObservableCollection<ListItem>();

 

                        this.fontSizeSingleListBox.ItemsSource = collection;

                    }

 

                    ListItem item = new ListItem(sizeString);

 

                    item.Tag = size;

 

                    collection.Insert(0, item);

                }

 

                this.fontSizeSingleListBox.Value = sizeString;

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - FontDialog()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public FontDialog()

        {

            InitializeComponent();

 

            this.fontFamilySingleListBox.ValueChanged  += fontFamilySingleListBox_ValueChanged;

            this.fontStyleSingleListBox.ValueChanged   += fontStyleSingleListBox_ValueChanged;

            this.fontWeightSingleListBox.ValueChanged  += fontWeightSingleListBox_ValueChanged;

            this.fontStretchSingleListBox.ValueChanged += fontStretchSingleListBox_ValueChanged;

            this.fontSizeSingleListBox.ValueChanged    += fontSizeSingleListBox_ValueChanged;

 

            this.okButton.Click     += okButton_Click;

            this.cancelButton.Click += cancelButton_Click;

 

            SetFontFamilySingleListBoxData(string.Empty);

 

            SetFontSizeSingleListBoxData(string.Empty);

 

            this.fontFamilySingleListBox.Focus();

 

            this.isUpdateSuppressed = false;

 

            UpdateSample();

        }

 

        #endregion

 

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

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

 

        #region 폰트 패밀리 싱글 리스트 박스 값 변경시 처리하기 - fontFamilySingleListBox_ValueChanged(sender, e)

 

        /// <summary>

        /// 폰트 패밀리 싱글 리스트 박스 값 변경시 처리하기

        /// </summary>

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

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

        private void fontFamilySingleListBox_ValueChanged(object sender, EventArgs e)

        {

            string fontStyleValue   = this.fontStyleSingleListBox.Value;

            string fontWeightValue  = this.fontWeightSingleListBox.Value;

            string fontStretchValue = this.fontStretchSingleListBox.Value;

 

            this.isUpdateSuppressed = true;

 

            ListItem fontFamilyListItem = this.fontFamilySingleListBox.GetItem();

 

            SetFontStyleSingleListBoxData  (fontFamilyListItem == null ? null : fontFamilyListItem.Tag as FontFamily, fontStyleValue  );

            SetFontWeightSingleListBoxData (fontFamilyListItem == null ? null : fontFamilyListItem.Tag as FontFamily, fontWeightValue );

            SetFontStretchSingleListBoxData(fontFamilyListItem == null ? null : fontFamilyListItem.Tag as FontFamily, fontStretchValue);

 

            this.isUpdateSuppressed = false;

 

            UpdateSample();

        }

 

        #endregion

        #region 폰트 스타일 싱글 리스트 박스 값 변경시 처리하기 - fontStyleSingleListBox_ValueChanged(sender, e)

 

        /// <summary>

        /// 폰트 스타일 싱글 리스트 박스 값 변경시 처리하기

        /// </summary>

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

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

        private void fontStyleSingleListBox_ValueChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region 폰트 웨이트 싱글 리스트 박스 값 변경시 처리하기 - fontWeightSingleListBox_ValueChanged(sender, e)

 

        /// <summary>

        /// 폰트 웨이트 싱글 리스트 박스 값 변경시 처리하기

        /// </summary>

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

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

        private void fontWeightSingleListBox_ValueChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region 폰트 스트레치 싱글 리스트 박스 값 변경시 처리하기 - fontStretchSingleListBox_ValueChanged(sender, e)

 

        /// <summary>

        /// 폰트 스트레치 싱글 리스트 박스 값 변경시 처리하기

        /// </summary>

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

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

        private void fontStretchSingleListBox_ValueChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region 폰트 크기 싱글 리스트 박스 값 변경시 처리하기 - fontSizeSingleListBox_ValueChanged(sender, e)

 

        /// <summary>

        /// 폰트 크기 싱글 리스트 박스 값 변경시 처리하기

        /// </summary>

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

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

        private void fontSizeSingleListBox_ValueChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region OK 버튼 클릭시 처리하기 - okButton_Click(sender, e)

 

        /// <summary>

        /// OK 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void okButton_Click(object sender, RoutedEventArgs e)

        {

            DialogResult = true;

        }

 

        #endregion

        #region Cancel 버튼 클릭시 처리하기 - cancelButton_Click(sender, e)

 

        /// <summary>

        /// Cancel 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void cancelButton_Click(object sender, RoutedEventArgs e)

        {

            DialogResult = false;

        }

 

        #endregion

 

        #region 싱글 리스트 박스 값 설정하기 - SetSingleListBoxValue(singleListBox, value)

 

        /// <summary>

        /// 싱글 리스트 박스 값 설정하기

        /// </summary>

        /// <param name="singleListBox">싱글 리스트 박스</param>

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

        private void SetSingleListBoxValue(SingleListBox singleListBox, string value)

        {

            ObservableCollection<ListItem> collection = singleListBox.ItemsSource as ObservableCollection<ListItem>;

 

            if(collection == null || collection.Count == 0)

            {

                return;

            }

 

            if(singleListBox.Contains(value))

            {

                singleListBox.Value = value;

            }

            else

            {

                singleListBox.Value = singleListBox.GetItem(0).Value;

            }

        }

 

        #endregion

 

        #region 폰트 패밀리 싱글 리스트 박스 데이터 설정하기 - SetFontFamilySingleListBoxData(value)

 

        /// <summary>

        /// 폰트 패밀리 싱글 리스트 박스 데이터 설정하기

        /// </summary>

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

        private void SetFontFamilySingleListBoxData(string value)

        {

            this.fontFamilySingleListBox.ItemsSource = UIHelper.GetFontFamilyData();

 

            SetSingleListBoxValue(this.fontFamilySingleListBox, value);

        }

 

        #endregion

        #region 폰트 스타일 싱글 리스트 박스 데이터 설정하기 - SetFontStyleSingleListBoxData(fontFamily, value)

 

        /// <summary>

        /// 폰트 스타일 싱글 리스트 박스 데이터 설정하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

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

        private void SetFontStyleSingleListBoxData(FontFamily fontFamily, string value)

        {

            if(fontFamily == null)

            {

                this.fontStyleSingleListBox.ItemsSource = null;

            }

            else

            {

                this.fontStyleSingleListBox.ItemsSource = UIHelper.GetFontStyleData(fontFamily);

 

                SetSingleListBoxValue(this.fontStyleSingleListBox, value);

            }

        }

 

        #endregion

        #region 폰트 웨이트 싱글 리스트 박스 데이터 설정하기 - SetFontWeightSingleListBoxData(fontFamily, value)

 

        /// <summary>

        /// 폰트 웨이트 싱글 리스트 박스 데이터 설정하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

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

        private void SetFontWeightSingleListBoxData(FontFamily fontFamily, string value)

        {

            if(fontFamily == null)

            {

                this.fontWeightSingleListBox.ItemsSource = null;

            }

            else

            {

                this.fontWeightSingleListBox.ItemsSource = UIHelper.GetFontWeightData(fontFamily);

 

                SetSingleListBoxValue(this.fontWeightSingleListBox, value);

            }

        }

 

        #endregion

        #region 폰트 스트레치 싱글 리스트 박스 데이터 설정하기 - SetFontStretchSingleListBoxData(fontFamily, value)

 

        /// <summary>

        /// 폰트 스트레치 싱글 리스트 박스 데이터 설정하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

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

        private void SetFontStretchSingleListBoxData(FontFamily fontFamily, string value)

        {

            if(fontFamily == null)

            {

                this.fontStretchSingleListBox.ItemsSource = null;

            }

            else

            {

                this.fontStretchSingleListBox.ItemsSource = UIHelper.GetFontStretchData(fontFamily);

 

                SetSingleListBoxValue(this.fontStretchSingleListBox, value);

            }

        }

 

        #endregion

        #region 폰트 크기 싱글 리스트 박스 데이터 설정하기 - SetFontSizeSingleListBoxData(value)

 

        /// <summary>

        /// 폰트 크기 싱글 리스트 박스 데이터 설정하기

        /// </summary>

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

        private void SetFontSizeSingleListBoxData(string value)

        {

            this.fontSizeSingleListBox.ItemsSource = UIHelper.GetFontSizeData();

 

            SetSingleListBoxValue(this.fontSizeSingleListBox, value);

        }

 

        #endregion

 

        #region 샘플 갱신하기 - UpdateSample()

 

        /// <summary>

        /// 샘플 갱신하기

        /// </summary>

        private void UpdateSample()

        {

            if(this.isUpdateSuppressed)

            {

                return;

            }

 

            ListItem fontFamilyListItem = this.fontFamilySingleListBox.GetItem();

 

            if(fontFamilyListItem == null)

            {

                this.displayLabel.Content = null;

 

                return;

            }

 

            ListItem fontStyleListItem = this.fontStyleSingleListBox.GetItem();

 

            if(fontStyleListItem == null)

            {

                this.displayLabel.Content = null;

 

                return;

            }

 

            ListItem fontWeightListItem = this.fontWeightSingleListBox.GetItem();

 

            if(fontWeightListItem == null)

            {

                this.displayLabel.Content = null;

 

                return;

            }

 

            ListItem fontStretchListItem = this.fontStretchSingleListBox.GetItem();

 

            if(fontStretchListItem == null)

            {

                this.displayLabel.Content = null;

 

                return;

            }

 

            ListItem fontSizeListItem = this.fontSizeSingleListBox.GetItem();

 

            if(fontSizeListItem == null)

            {

                this.displayLabel.Content = null;

 

                return;

            }

 

            this.displayLabel.Content = "가나다라마 ABC abc 12345";

 

            this.displayLabel.FontFamily  = fontFamilyListItem.Tag as FontFamily;

            this.displayLabel.FontStyle   = (FontStyle)fontStyleListItem.Tag;

            this.displayLabel.FontWeight  = (FontWeight)fontWeightListItem.Tag;

            this.displayLabel.FontStretch = (FontStretch)fontStretchListItem.Tag;

            this.displayLabel.FontSize    = (double)fontSizeListItem.Tag / 0.75d;

        }

 

        #endregion

    }

}

 

 

MainWindow.cs

 

 

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            Width      = 800d;

            Height     = 600d;

            FontFamily = new FontFamily("나눔고딕코딩");

            FontSize   = 16d;

            Title      = "폰트 대화 상자 사용하기";

 

            Button chooseButton = new Button();

 

            chooseButton.HorizontalAlignment = HorizontalAlignment.Center;

            chooseButton.VerticalAlignment   = VerticalAlignment.Center;

            chooseButton.Padding             = new Thickness(10);

            chooseButton.Content             = "폰트 선택";

 

            chooseButton.Click += chooseButton_Click;

 

            Content = chooseButton;

        }

 

        #endregion

 

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

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

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

 

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        private static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

 

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

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

 

        #region 선택 버튼 클릭시 처리하기 - chooseButton_Click(sender, e)

 

        /// <summary>

        /// 선택 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void chooseButton_Click(object sender, RoutedEventArgs e)

        {

            FontDialog fontDialog = new FontDialog();

 

            fontDialog.Owner = this;

 

            fontDialog.Typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);

            fontDialog.FaceSize = FontSize;

 

            if(fontDialog.ShowDialog().GetValueOrDefault())

            {

                FontFamily  = fontDialog.Typeface.FontFamily;

                FontStyle   = fontDialog.Typeface.Style;

                FontWeight  = fontDialog.Typeface.Weight;

                FontStretch = fontDialog.Typeface.Stretch;

                FontSize    = fontDialog.FaceSize;

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요