question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot set Dependency Property of the FontWeight? (Nullable<FontWeight>) type in XAML

See original GitHub issue

Describe the bug

    public class MyControl : Control {
        public static readonly DependencyProperty NullableStructProperty = DependencyProperty.Register("NullableStruct", typeof(FontWeight?), typeof(MyControl), new PropertyMetadata(null));
        public FontWeight? NullableStruct { get { return (FontWeight?)GetValue(NullableStructProperty); } set { SetValue(NullableStructProperty, value); } }
    }
<local:MyControl x:Name="c3" NullableStruct="Bold"/>

Result: ‘XAML parsing failed.’ 'Failed to create a ‘Windows.Foundation.IReference`1<Windows.UI.Text.FontWeight>’ from the text ‘Bold’.

Reproduced in any version, including 1.0.0-preview2

Steps to reproduce the bug

Run sample App1.zip

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 0.8: 0.8.0 (If you’re seeing your issue in older previews of WinUI 3, please try this release)

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

May 2021 Update (19043)

Additional context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexdi220commented, Oct 26, 2021

It doesn’t work for non-dependency (simple) properties too:

public FontWeight? FontWeight { get; set; }
<local:MyStyle FontWeight="Bold"/> <!-- cause exception -->
0reactions
RealTommyKleincommented, Oct 25, 2021

This looks like a bug where the framework doesn’t support unboxing FontWeight from a string (for MSFT employees, this code needs a valueString case to create a FontWeight from a string).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Property - Unable to Set Value From XAML
When I add that code it says that the string PassColor is invalid. Without the string there at all, the code compiles and...
Read more >
WPF set from DependencyProperty not set a value if DP is ...
Meaning, no, you cannot ensure the set accessor is called. It never will be. This is not a true. Of course, never will...
Read more >
TextElement.FontWeight Property
Gets or sets the top-level font weight for the content of the element. ... This dependency property also has an attached property usage....
Read more >
Can't Set WPF Custom Control Dependency Property from ...
Hello,. I've created a "nav button" custom control, with the goal of simply passing it an icon path, and then the text that...
Read more >
Dependency Properties | WPF Fundamentals
WPF introduces a type of property called a dependency property that is used throughout the platform to enable styling, automatic data ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found