I want to create a DependencyProperty
in an extension class for FrameworkElement
, and bind a xaml property to it.
The idea comes from the source code of the Windows Community Toolkit : https://github.com/windows-toolkit/WindowsCommunityToolkit
They are using extensions to add a DependencyProperty
to FrameworkElement
, in FrameworkElementExtensions.ActualSize.cs file.
I can use their extension without any problem, however when I try to do the same thing myself, I get a Windows.UI.Xaml.Markup.XamlParseException: 'XAML parsing failed.'
I even tried copy/pasting their extension class and the problem persists.
My project is a UWP project.
Here is some simple code to test it out:
<UserControl
xmlns:myExtensions="using:MyProject.Extensions"
xmlns:toolkitExtensions="using:Microsoft.Toolkit.Uwp.UI.Extensions"
>
<Grid>
<Grid x:Name="TestElementName"
myExtensions:FrameworkElementExtensions.CustomProperty="0.5"
toolkitExtensions:FrameworkElementExtensions.EnableActualSizeBinding="true"
/>
<Grid Width="{Binding ElementName=TestElementName, Path=(toolkitExtensions:FrameworkElementExtensions.ActualWidth)}"
Height="{Binding ElementName=TestElementName, Path=(myExtensions:FrameworkElementExtensions.ActualHeight)}"
Opacity="{Binding ElementName=TestElementName, Path=(myExtensions:FrameworkElementExtensions.CustomProperty)}"
/>
</Grid>
</UserControl>
namespace MyProject.Extensions
{
public static partial class FrameworkElementExtensions
{
public static readonly DependencyProperty ActualHeightProperty = DependencyProperty.RegisterAttached("ActualHeight", typeof(double), typeof(FrameworkElement), new PropertyMetadata(double.NaN));
public static double GetActualHeight(FrameworkElement obj)
{
return (double)obj.GetValue(ActualHeightProperty);
}
public static void SetActualHeight(FrameworkElement obj, double value)
{
obj.SetValue(ActualHeightProperty, value);
}
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElement), new PropertyMetadata(1.0));
public static double GetCustomProperty(FrameworkElement obj)
{
return (double)obj.GetValue(CustomPropertyProperty);
}
public static void SetCustomProperty(FrameworkElement obj, double value)
{
obj.SetValue(CustomPropertyProperty, value);
}
}
}
The ActualHeight property is copy/pasted from Windows Community Toolkit
The CustomProperty property is mine
Assignment in the 1st Grid works correctly, but bindings to both Height and Opacity in the 2nd Grid throw an exception.
I don't see how it can work when used from the Community Toolkit namespace and not from mine.
Details from the exception:
Type:
Windows.UI.Xaml.Markup.XamlParseException
Message:
XAML parsing failed.
Stacktrace:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at MyProject.Controls.MyControl.InitializeComponent()
at MyProject.Controls.MyControl..ctor()
at MyProject.MyProject_XamlTypeInfo.XamlTypeInfoProvider.Activate_271_MyControl()
at MyProject.MyProject_XamlTypeInfo.XamlUserType.ActivateInstance()
InnerException is null.
Can anyone help me with this?
Thank you.