Custom DependencyProperty in MainWindow class in WinUI 3

Alasdair Turnbull 21 Reputation points
2022-05-09T13:35:00.183+00:00

Dear Community,

I'm trying to migrate a WPF application to WinUI 3 but I'm stymied at simply defining a DependencyProperty in my application's main UI window class. Here's what I wrote:

using Microsoft.UI.Xaml;

    public sealed partial class MainWindow : Window  
    {  
        public static readonly DependencyProperty RootFontSizeProperty = DependencyProperty.Register("RootFontSize",  
                                                                        typeof(double), typeof(MainWindow),   
                                                                        new PropertyMetadata(8.0));  
        public double RootFontSize  
        {  
            get { return (double)GetValue(RootFontSizeProperty); }  
            set { SetValue(RootFontSizeProperty, value); }  
        }  
    }  
  

The problem is, neither MainWindow nor Window contain GetValue() or SetValue().

If I understand the documentation correctly (https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/custom-dependency-properties), MainWindow must be derived from DependencyObject somewhere in its past. According to https://learn.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.dependencyobject?view=winui-3.0, Microsoft.UI.Xaml.Window is, so MainWindow should be, too.

How do I add a custom DependencyProperty to MainWindow?

Thanks for any help.

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.
728 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,826 Reputation points
    2022-05-09T18:32:05.24+00:00

    From microsoft.ui.xaml.window, Window does not derive from DependencyObject (or from "Go to Definition" in the code)
    like WPF Window

    Maybe you could use the Root of the window (usually a StackPanel or a Grid in WinUI...) :

            public double RootFontSize  
            {  
                get { return (double)this.Content.GetValue(RootFontSizeProperty); }  
                set { this.Content.SetValue(RootFontSizeProperty, value); }  
            }  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alasdair Turnbull 21 Reputation points
    2022-05-09T19:35:16.113+00:00

    Thank you for your reply. Oddly, the WinUI 3.0 documentation lists a bunch of classes that derive from DependencyObject and says "Window: represents the main app window." is one. As you point out, there's no evidence of this in the code or object browser.

    I figured I was missing something but Microsoft.UI.Xaml.Data.Binding specifically says (somewhere) that it only binds to FrameworkElement (in WinUI 3, anyway). I think I'll try your approach and use MainWindow.Content.

    Thanks again.

    0 comments No comments