■ INotifyPropertyChanged 인터페이스를 구현한 데이터 소스 객체 만들기

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

ClockTicker.cs

 

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;

/// <summary>
///
시계 틱 발생자
/// </summary>
public class ClockTicker : INotifyPropertyChanged
{
   
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
   ////////////////////////////////////////////////////////////////////////////////////////// Public

   #region 속성 변경시 - PropertyChanged

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

   #endregion

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

   #region 일시 - DateTime

   
/// <summary>
   /// 일시
   /// </summary>
   public DateTime DateTime
   {
       
get
       {
           
return DateTime.Now;
       }
   }

   #endregion

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

   #region 생성자 - ClockTicker()

   
/// <summary>
   /// 생성자
   /// </summary>
   public ClockTicker()
   {
       
DispatcherTimer dispatcherTimer = new DispatcherTimer();

       dispatcherTimer.Tick += dispatcherTimer_Tick;

       dispatcherTimer.Interval =
TimeSpan.FromSeconds(1);

       dispatcherTimer.Start();
   }

   #endregion

   //////////////////////////////////////////////////////////////////////////////////////////////////// Method
   ////////////////////////////////////////////////////////////////////////////////////////// Private

   #region 디스패처 타이머 틱 처리하기 - dispatcherTimer_Tick(sender, e)

   
/// <summary>
   /// 디스패처 타이머 틱 처리하기
   /// </summary>
   /// <param name="sender">이벤트 발생자</param>
   /// <param name="e">이벤트 인자</param>
   private void dispatcherTimer_Tick(object sender, EventArgs e)
   {
       
if(PropertyChanged != null)
       {
           PropertyChanged(
this, new PropertyChangedEventArgs("DateTime"));
       }
   }

   #endregion
}

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

/// <summary>
///
포맷 텍스트 값 변환자
/// </summary>
public class FormattedTextValueConverter : 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">CultureInfo</param>
   /// <returns>변환 값</returns>
   public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)
   {
       
if(parameter is string)
       {
           
return string.Format(parameter as string, sourceValue);
       }

       
return sourceValue.ToString();
   }

   #endregion

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

   
/// <summary>
   /// 역변환하기
   /// </summary>
   /// <param name="sourceValue">소스 값</param>
   /// <param name="targetType">타겟 타입</param>
   /// <param name="parameter">파라미터</param>
   /// <param name="cultureInfo">CultureInfo</param>
   /// <returns>역변환 값</returns>
   public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)
   {
       
return null;
   }

   #endregion
}

 

TestWindow.xaml

<Window
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:src="clr-namespace:TestProject"
   SizeToContent="WidthAndHeight"
   ResizeMode="CanMinimize"
   FontFamily="Bookman Old Style"
   FontSize="36pt"
   Title="디지털 시계">
   <Window.Resources>
       <src:ClockTicker x:Key="ClockTickerKey" />
       <src:FormattedTextValueConverter x:Key="FormattedTextValueConverterKey" />
   </Window.Resources>
   <Window.Content>
       <Binding
           Source="{StaticResource ClockTickerKey}"
           Path="DateTime"
           Converter="{StaticResource FormattedTextValueConverterKey}"
           ConverterParameter="{}{0:T}" />
   </Window.Content>
</
Window>
----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG