Difference between Dependency Property and Normal Property

budding_developer 156 Reputation points
2022-10-03T06:32:52.2+00:00

Hi, we are able to bind both normal property and dependency property of a usercontrol from another page/usercontrol in WinUI 3. What is the actual difference between dependency properties and normal properties other than PropertyChangedCallback. Which will be better in the performance aspect?

Sample code:

Binding UserControl properties from Window:

<Window  
    x:Class="App1.MainWindow"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="using:App1"  
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    mc:Ignorable="d">  
  
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">  
        <local:UserControl1 NormalPropertyText="Normal Text" DependencyPropertyText="Dependency Property Text"/>  
    </StackPanel>  
</Window>  

UserControl with Dependency property and Normal property:

public sealed partial class UserControl1 : UserControl  
    {  
        public UserControl1()  
        {  
            this.InitializeComponent();  
        }  
  
        public string NormalPropertyText { get; set; }  
  
        public string DependencyPropertyText  
        {  
            get { return (string)GetValue(DP); }  
            set { SetValue(DP, value); }  
        }  
  
        public static readonly DependencyProperty DP = DependencyProperty.Register(nameof(DependencyPropertyText), typeof(string), typeof(UserControl1), new PropertyMetadata(null));  
    }  

Thanks in Advance :)

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
752 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,231 Reputation points Microsoft Vendor
    2022-10-03T07:44:19.983+00:00

    See When to implement a property as a dependency property,
    You might consider implementing your property as a dependency property when you want it to support one or more of these features of the Windows Runtime or of Windows Runtime apps:
    It's preferred to use dependency properties with Windows Runtime features but normal properties behave as a language feature.


0 additional answers

Sort by: Most helpful