첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

TestProject.zip
다운로드

▶ SimpleElement.cs

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

<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>
728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요