Addowner wpf gleiche dependency property

첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

Addowner wpf gleiche dependency property

TestProject.zip

다운로드

▶ SpaceButton.cs

using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 공백 버튼
    /// </summary>
    public class SpaceButton : Button
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 공백 속성
        /// </summary>
        public static readonly DependencyProperty SpaceProperty;

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 제목
        /// </summary>
        private string caption;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 제목 - Caption

        /// <summary>
        /// 제목
        /// </summary>
        public string Caption
        {
            get
            {
                return this.caption;
            }
            set
            {
                this.caption = value;

                Content = GetContent(this.caption);
            }
        }

        #endregion
        #region 공백 - Space

        /// <summary>
        /// 공백
        /// </summary>
        public int Space
        {
            set
            {
                SetValue(SpaceProperty, value);
            }
            get
            {
                return (int)GetValue(SpaceProperty);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Static

        #region 생성자 - SpaceButton()

        /// <summary>
        /// 생성자
        /// </summary>
        static SpaceButton()
        {
            FrameworkPropertyMetadata frameworkPropertyMetadata = new FrameworkPropertyMetadata();

            frameworkPropertyMetadata.Inherits       = true;
            frameworkPropertyMetadata.AffectsMeasure = true;
            frameworkPropertyMetadata.DefaultValue   = 1;

            frameworkPropertyMetadata.PropertyChangedCallback += metadata_PropertyChangedCallback;

            SpaceProperty = DependencyProperty.Register
            (
                "Space",
                typeof(int),
                typeof(SpaceButton),
                frameworkPropertyMetadata,
                SpacePropertyValidateValueCallback
            );
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private
        ////////////////////////////////////////////////////////////////////// Event

        #region 메타 데이터 속성 변경시 콜백 처리하기 - metadata_PropertyChangedCallback(dependencyObject, e)

        /// <summary>
        /// 메타 데이터 속성 변경시 콜백 처리하기
        /// </summary>
        /// <param name="dependencyObject">의존 객체</param>
        /// <param name="e">이벤트 인자</param>
        private static void metadata_PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            SpaceButton button = dependencyObject as SpaceButton;

            button.Content = button.GetContent(button.caption);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////// Function

        #region 공백 속성 값 검증시 콜백 처리하기 - SpacePropertyValidateValueCallback(sourceValue)

        /// <summary>
        /// 공백 속성 값 검증시 콜백 처리하기
        /// </summary>
        /// <param name="sourceValue">소스 값</param>
        /// <returns>검증 결과</returns>
        private static bool SpacePropertyValidateValueCallback(object sourceValue)
        {
            int i = (int)sourceValue;

            return i >= 0;
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private
        ////////////////////////////////////////////////////////////////////// Function

        #region 컨텐트 구하기 - GetContent(source)

        /// <summary>
        /// 컨텐트 구하기
        /// </summary>
        /// <param name="source">소스 문자열</param>
        /// <returns>문자열</returns>
        private string GetContent(string source)
        {
            if(source == null)
            {
                return null;
            }

            StringBuilder stringBuilder = new StringBuilder();

            foreach(char character in source)
            {
                stringBuilder.Append(character + new string(' ', Space));
            }

            return stringBuilder.ToString().Trim();
        }

        #endregion
    }
}

▶ SpaceWindow.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 공백 윈도우
    /// </summary>
    public class SpaceWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 공백 속성
        /// </summary>
        public static readonly DependencyProperty SpaceProperty;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 공백 - Space

        /// <summary>
        /// 공백
        /// </summary>
        public int Space
        {
            set
            {
                SetValue(SpaceProperty, value);
            }
            get
            {
                return (int)GetValue(SpaceProperty);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Static

        #region 생성자 - SpaceWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        static SpaceWindow()
        {
            SpaceProperty = SpaceButton.SpaceProperty.AddOwner(typeof(SpaceWindow));

            FrameworkPropertyMetadata frameworkPropertyMetadata = new FrameworkPropertyMetadata();

            frameworkPropertyMetadata.Inherits = true;

            SpaceProperty.OverrideMetadata(typeof(SpaceWindow), frameworkPropertyMetadata);
        }

        #endregion
    }
}

▶ MainWindow.xaml

<local:SpaceWindow x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="DependencyProperty 클래스 : AddOwner 메소드를 사용해 다른 소유자 추가하기"
    FontFamily="나눔고딕코딩"
    FontSize="16"
    Space="3">
    <Grid>
        <local:SpaceButton
            Width="200"
            Height="30"
            Caption="테스트" />
    </Grid>
</local:SpaceWindow>

▶ MainWindow.xaml.cs

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : SpaceWindow
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion
    }
}

What is WPF dependency property?

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a type's property. Collectively, these services are referred to as the WPF property system. A property that's backed by the WPF property system is known as a dependency property.

What is dependency property in WPF interview?

Dependency property is a special kind of property introduced in WPF. This property helps in data binding the source and target object. Dependency property has been designed to reduce the memory footprint. These properties are static in nature.

Where is dependency property in WPF?

Dependency properties are used when you want data binding in a UserControl , and is the standard method of data binding for the WPF Framework controls. DPs have slightly better binding performance, and everything is provided to you when inside a UserControl to implement them.

Why dependency property is static in WPF?

DependencyProperty has to be static (Class level) because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty.