question

AlasdairTurnbull-6265 avatar image
0 Votes"
AlasdairTurnbull-6265 asked AlasdairTurnbull-6265 answered

Custom DependencyProperty in MainWindow class in WinUI 3

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://docs.microsoft.com/en-us/windows/uwp/xaml-platform/custom-dependency-properties), MainWindow must be derived from DependencyObject somewhere in its past. According to https://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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); }
         }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlasdairTurnbull-6265 avatar image
0 Votes"
AlasdairTurnbull-6265 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.