■ 커스텀 엘리먼트 바인딩을 사용하는 방법을 보여준다.
▶ SimpleElement.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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
using System.Globalization; using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 단순 엘리먼트 /// </summary> public class SimpleElement : FrameworkElement { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 번호 속성 /// </summary> public static DependencyProperty NumberProperty; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 번호 - Number /// <summary> /// 번호 /// </summary> public double Number { set { SetValue(NumberProperty, value); } get { return (double)GetValue(NumberProperty); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - SimpleElement() /// <summary> /// 생성자 /// </summary> static SimpleElement() { NumberProperty = DependencyProperty.Register ( "Number", typeof(double), typeof(SimpleElement), new FrameworkPropertyMetadata ( 0.0, FrameworkPropertyMetadataOptions.AffectsRender ) ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 측정하기 (오버라이딩) - MeasureOverride(availableSize) /// <summary> /// 측정하기 (오버라이딩) /// </summary> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>희망 크기</returns> protected override Size MeasureOverride(Size availableSize) { return new Size(200, 50); } #endregion #region 렌더링 하기 - OnRender(drawingContext) /// <summary> /// 렌더링 하기 /// </summary> /// <param name="drawingContext">DrawingContext 객체</param> protected override void OnRender(DrawingContext drawingContext) { drawingContext.DrawText ( new FormattedText ( Number.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Times New Roman"), 12, SystemColors.WindowTextBrush ), new Point(0, 0) ); } #endregion } } |
▶ MainWindow.xaml
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 |
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:TestProject" Width="800" Height="600" Title="커스텀 엘리먼트 바인딩 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel> <ScrollBar Margin="24" Orientation="Horizontal" Maximum="100" SmallChange="1" LargeChange="10" Value="{Binding ElementName=simpleElement, Path=Number, Mode=OneWayToSource}" /> <src:SimpleElement x:Name="simpleElement" HorizontalAlignment="Center" /> <ScrollBar Name="scrollBar" Margin="24" Orientation="Horizontal" Maximum="100" SmallChange="1" LargeChange="10" Value="{Binding ElementName=simpleElement, Path=Number, Mode=TwoWay}" /> <src:SimpleElement HorizontalAlignment="Center" Number="{Binding ElementName=scrollBar, Path=Value, Mode=OneWay}" /> </StackPanel> </Window> |