I have a WinUI/UWP project. In my custom control I have a property of type Type. I tried to set it from xaml like ControlTemplate.TargetType property, but got an exception on InitializeComponent.
Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.
Failed to assign to property 'App11.MyControl.SomeProperty'. [Line: 17 Position: 26]'
Is there a way to set such properties from xaml?
Sample control
public class MyControl : Button
{
public Type SomeProperty { get; set; }
}
Xaml
<local:MyControl SomeProperty="Button" />
we don't have x:Type in WinUI. @MikeHillberg is there a way to achieve this ?
I don't think so, and I can't find an existing Issue on it. It's supported for built-in properties (like Style.TargetType) but missing for custom properties. @alwu-msft?
This works for me (my test app) whether the custom property of type System.Type is a DependencyProperty or a regular C# property.
@BorzillaR can you attach your repro project? It's possible my test app isn't doing the same thing as yours and that is responsible for the difference in behavior.
@alwu-msft, seems you created a classic UWP project and it works on my side. I created the same project for WinUI/UWP and it fails. Looks like a bug.
Thanks! You're right, the Windows.UI.Xaml.Markup.XamlParseException text displayed by VS tricked me into thinking that you were using UWP XAML rather than WinUI 3.
As it turns out, this is a known issue that we're already working on fixing, but we appreciate the report nonetheless!
This will be fixed in the WinUI 3 Preview 2 release.
Most helpful comment
Thanks! You're right, the
Windows.UI.Xaml.Markup.XamlParseExceptiontext displayed by VS tricked me into thinking that you were using UWP XAML rather than WinUI 3.As it turns out, this is a known issue that we're already working on fixing, but we appreciate the report nonetheless!