■ RadioButton 클래스 : 열거형 바인딩하기

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


TestProject.zip


Effect.cs

 

 

namespace TestProject

{

    /// <summary>

    /// 효과

    /// </summary>

    public enum Effect

    {

        /// <summary>

        /// 해당 무

        /// </summary>

        None,

 

        /// <summary>

        /// 그림자

        /// </summary>

        Shadow,

 

        /// <summary>

        /// 외곽선

        /// </summary>

        Outline

    }

}

 

 

EffectModel.cs

 

 

using System.ComponentModel;

 

namespace TestProject

{

    /// <summary>

    /// 효과 모델

    /// </summary>

    public class EffectModel : INotifyPropertyChanged

    {

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

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

 

        #region 속성 변경시 이벤트 - PropertyChanged

 

        /// <summary>

        /// 속성 변경시 이벤트

        /// </summary>

        public event PropertyChangedEventHandler PropertyChanged;

 

        #endregion

 

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

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

 

        #region Field

 

        /// <summary>

        /// 효과

        /// </summary>

        private Effect effect;

 

        #endregion

 

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

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

 

        #region 효과 - Effect

 

        /// <summary>

        /// 효과

        /// </summary>

        public Effect Effect

        {

            get

            {

                return this.effect;

            }

            set

            {

                this.effect = value;

 

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Effect"));

            }

        }

 

        #endregion

    }

}

 

 

EffectToBooleanConverter.cs

 

 

using System;

using System.Globalization;

using System.Windows;

using System.Windows.Data;

 

namespace TestProject

{

    /// <summary>

    /// 효과↔불린형 변환자

    /// </summary>

    public class EffectToBooleanConverter : 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)

        {

            string parameterString = parameter as string;

 

            if(parameterString == null)

            {

                return DependencyProperty.UnsetValue;

            }

 

            if(Enum.IsDefined(value.GetType(), value) == false)

            {

                return DependencyProperty.UnsetValue;

            }

 

            object parameterValue = Enum.Parse(value.GetType(), parameterString);

 

            return parameterValue.Equals(value);

        }

 

        #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)

        {

            string parameterString = parameter as string;

 

            if(parameterString == null)

            {

                return DependencyProperty.UnsetValue;

            }

 

            return Enum.Parse(targetType, parameterString);

        }

 

        #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="RadioButton 클래스 : 열거형 바인딩하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Window.Resources>

        <local:EffectToBooleanConverter x:Key="EffectToBooleanConverterKey" />

    </Window.Resources>

    <Grid>

        <StackPanel

            HorizontalAlignment="Center"

            VerticalAlignment="Center">

            <RadioButton x:Name="radioButton1"

                Margin="10"

                Content="해당 무"

                IsChecked="{Binding Path=Effect, Converter={StaticResource EffectToBooleanConverterKey}, ConverterParameter=None}" />

            <RadioButton x:Name="radioButton2"

                Margin="10"

                Content="그림자"

                IsChecked="{Binding Path=Effect, Converter={StaticResource EffectToBooleanConverterKey}, ConverterParameter=Shadow}" />

            <RadioButton x:Name="radioButton3"

                Margin="10"

                Content="외곽선"

                IsChecked="{Binding Path=Effect, Converter={StaticResource EffectToBooleanConverterKey}, ConverterParameter=Outline}" />

            <Label

                Margin="10"

                Content="{Binding Path=Effect}" />

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            DataContext = new EffectModel();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG