첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ IValueConverter 인터페이스를 구현하는 기본적인 방법을 보여준다.

TestProject.zip
0.01MB

▶ SampleData.cs

using System;
using System.ComponentModel;

namespace TestProject
{
    /// <summary>
    /// 샘플 데이터
    /// </summary>
    public class SampleData : INotifyPropertyChanged
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Event
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 속성 변경시 - PropertyChanged

        /// <summary>
        /// 속성 변경시
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 일자
        /// </summary>
        private DateTime date;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 일자 - Date

        /// <summary>
        /// 일자
        /// </summary>
        public DateTime Date
        {
            get
            {
                return this.date;
            }
            set
            {
                this.date = value;

                FirePropertyChangedEvent("Date");
            }
        }

        #endregion

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

        #region 생성자 - SampleData()

        /// <summary>
        /// 생성자
        /// </summary>
        public SampleData()
        {
            this.date = DateTime.Now;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName)

        /// <summary>
        /// 속성 변경시 이벤트 발생시키기
        /// </summary>
        /// <param name="propertyName">속성명</param>
        private void FirePropertyChangedEvent(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

 

▶ SampleConverter.cs

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 샘플 변환자
    /// </summary>
    public class SampleConverter : IValueConverter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

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

        /// <summary>
        /// 변환하기
        /// </summary>
        /// <param name="sourceValue">소스 값</param>
        /// <param name="targetType">타겟 타입</param>
        /// <param name="parameter">매개 변수</param>
        /// <param name="cultureInfo">문화 정보</param>
        /// <returns>변환 값</returns>
        public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)
        {
            DateTime date = (DateTime) sourceValue;

            switch(targetType.Name)
            {
                case "String" : return date.ToString("F", cultureInfo);
                case "Brush"  : return Brushes.Red;
                default       : return sourceValue;
            }
        }

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

        /// <summary>
        /// 역변환하기
        /// </summary>
        /// <param name="sourceValue">소스 값</param>
        /// <param name="targetType">타겟 타입</param>
        /// <param name="parameter">매개 변수</param>
        /// <param name="cultureInfo">문화 정보</param>
        /// <returns>역변환 값</returns>
        public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) => null;

        #endregion
    }
}

 

▶ MainWindow.xml

<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="TestProject"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <local:SampleData x:Key="SampleDataKey" />
        <local:SampleConverter x:Key="SampleConverterKey" />
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10" />
        </Style>
    </Window.Resources>
    <Grid
        HorizontalAlignment="Center"
        VerticalAlignment="Center">
        <Grid.DataContext>
            <Binding Source="{StaticResource SampleDataKey}" />
        </Grid.DataContext>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0"
            HorizontalAlignment="Right"
            FontWeight="Bold"
            Text="Unconverted data" />
        <TextBlock Grid.Row="0" Grid.Column="1"
            Text="{Binding Path=Date}" />
        <TextBlock Grid.Row="1" Grid.Column="0"
            HorizontalAlignment="Right"
            FontWeight="Bold"
            Text="Converted data" />
        <TextBlock Grid.Row="1" Grid.Column="1"
            Foreground="{Binding Path=Date, Converter={StaticResource SampleConverterKey}}">
            <TextBlock.Text>
                <Binding
                    Path="Date"
                    Converter="{StaticResource SampleConverterKey}" />
            </TextBlock.Text>
        </TextBlock>
    </Grid>
</Window>
728x90
그리드형(광고전용)
Posted by icodebroker
,