■ IMultiValueConverter 인터페이스를 구현해 사각형 변환자로 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System; using System.Globalization; using System.Linq; using System.Windows; using System.Windows.Data; /// <summary> /// 사각형 변환자 /// </summary> public class RectangleConverter : IMultiValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValueArray, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValueArray">소스 값 배열</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object[] sourceValueArray, Type targetType, object parameter, CultureInfo cultureInfo) { if(sourceValueArray.Any(o => o == DependencyProperty.UnsetValue || o == null)) { return null; } double width = (double)sourceValueArray[0]; double height = (double)sourceValueArray[1]; return new Rect(0, 0, width, height); } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetTypeArray, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetTypeArray">타겟 타입 배열</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object[] ConvertBack(object sourceValue, Type[] targetTypeArray, object parameter, CultureInfo cultureInfo) { throw new NotImplementedException(); } #endregion } |