■ IValueConverter 인터페이스를 구현하는 기본적인 방법을 보여준다.
▶ SampleData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<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> |