01. IValueConverter를 상속받아서 구현한 자신만의 ValueConverter 클래스를 만든다.
(여기서는 MyConverter라는 이름을 사용한다.)
9 public class MyConverter : IValueConverter
10 {
11 #region IValueConverter Members
12
13 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
14 {
15 return "";
16 }
17
18 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
19 {
20 return "";
21 }
22
23 #endregion
24 }
02. 자신이 만든 ValueConverter를 다음과 같이 Xaml의 Resource으로 등록 한다.
<Window ... xmlns:src="clr-namespace:ValueConverterTest">
<Window.Resources>
<src:MyConverter x:Key="MyConverter" />
</Window.Resources>
03. 마지막으로 사용할 곳에 StaticResource으로 사용한다.
<TextBlock Text="{Binding SomePath, Converter={StaticResource MyConverter}}" />
위 방법은 대부분 사용하는 방법인데 의문이 나는 것은 "Resource로 등록하지 않고 사용하는 방법이 없을까?"라는 것이다. 다행이 Dr.WPF 포스트에 이 방법이 설명이 되어 있었습니다.
사용자가 만든 ValueConverter에 MarkupExtension 클래스를 상속 받아서 ProvideValue를 오버라이드 하면 된다.
10 public class MyConverter : MarkupExtension, IValueConverter
11 {
12 #region IValueConverter Members
13
14 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
15 {
16 return "";
17 }
18
19 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
20 {
21 return "";
22 }
23
24 #endregion
25
26 private static MyConverter _converter = null;
27 public override object ProvideValue(IServiceProvider serviceProvider)
28 {
29 if (_converter == null)
30 {
31 _converter = new MyConverter();
32 }
33 return _converter;
34 }
35 }
위와 같이 ValueConverter를 선언을 하면 따로 리소스로 등록 할 필요 없이 다음과 같이 사용하면 된다.
<TextBlock Text="{Binding SomePath, Converter={src:MyConverter}}" />




Leave your greetings here.