728x90
728x170

Custom FrameworkElement 정의하고 사용하는 방법입니다.

NumberProperty(숫자)를 의존속성으로가지며 이를 화면에 보여주는 FrameworkElement 를 정의합니다.

 

CustomElement.cs

using System.Globalization;

using System.Windows;

using System.Windows.Media;

 

namespace WpfApp

{

    class CustomElement : FrameworkElement

    {

        /// <summary>

        /// 화면에 표시될 숫자에 대한 의존 속성입니다.

        /// </summary>

        public static DependencyProperty NumberProperty;

 

        /// <summary>

        /// 화면에 표시될 숫자 값입니다.

        /// </summary>

        public double Number

        {

            set

            {

                SetValue(NumberProperty, value);

            }

            get

            {

                return (double)GetValue(NumberProperty);

            }

        }

 

        /// <summary>

        /// 생성자입니다.

        /// </summary>

        static CustomElement()

        {

            // DependencyProperty 를 생성합니다.

            NumberProperty = DependencyProperty.Register

            (

                "Number",

                typeof(double),

                typeof(CustomElement),

                new FrameworkPropertyMetadata

                (

                    0.0,

                    FrameworkPropertyMetadataOptions.AffectsRender

                )

            );

        }

 

 

        /// <summary>

        /// FrameworkElement 의 크기를 정의합니다.

        /// </summary>

        /// <param name="sizeAvailable">자식 요소에 제공할 수 있는 사용 가능한 크기입니다.</param>

        /// <returns></returns>

        protected override Size MeasureOverride(Size sizeAvailable)

        {

            return new Size(200, 50);

        }

 

        /// <summary>

        /// 레이아웃시스템에서 감독하는 렌더링 작업에 참여합니다.

        /// Number 를 화면에 표시합니다.

        /// </summary>

        /// <param name="dc">특정 요소에 대 한 그리기 지침입니다</param>

        protected override void OnRender(DrawingContext dc)

        {

            SolidColorBrush mySolidColorBrush = new SolidColorBrush() { Color = Colors.LimeGreen };

            Pen myPen = new Pen(Brushes.LimeGreen, 1);

            Rect myRect = new Rect(0, 0, 200, 50);

 

            dc.DrawRectangle(mySolidColorBrush, myPen, myRect);

 

            dc.DrawText

            (

                new FormattedText

                (

                    Number.ToString(),

                    CultureInfo.CurrentCulture,

                    FlowDirection.LeftToRight,

                    new Typeface("Arial"),

                    12,

                    SystemColors.WindowTextBrush

                ),

                new Point(0, 0)

            );

        }

    }

}

 

 

정의한 CustomElement 를 아래와 같이 사용하면 Number 에 정의한 내용이 화면에 표시됩니다.

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:src="clr-namespace:WpfApp"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <DockPanel>

        <src:CustomElement Number="50"/>

    </DockPanel>

</Window>

 

 

 

728x90
그리드형
Posted by kjun
,