I wish to use AngelSix's custom attached properties Code in a .NET Framework 4.7.2 class library. However, when I try to add an attached property to an object in a Xaml file, what I get from Intellisense in Visual Studio is local:BaseAttachedProperty`2.value
and not the desired attached property like local:IsBusyProperty.Value
. When I manually type local:IsBusyProperty.Value
, the application crashes with the error message The method or operation is not implemented
pointing to the attached property.
I understand that the `2
means a generic type, with 2 generic parameters - alluding to the Base Class with two generic parameters:
public abstract class BaseAttachedProperty<Parent, Property>
where Parent : new(){}
But how do I get the correct attached property, that is, classes that implement the base class with generic parameters and not this cryptic local:BaseAttachedProperty`2.value
message?
If I create an Attached Property without inheriting from the BaseAttachedProperty, everything works fine.
A user on a German Forum had the same problem and writes that he could use xmlns:
to get the attached properties but this does not work for me!
Here is a sample that AngelSix provided. It will not work for me.
public class IsBusyProperty : BaseAttachedProperty<IsBusyProperty, bool>
{
}
Here is my code which works:
public class IsBusyProperty
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.RegisterAttached("Value", typeof(bool), typeof(IsBusyProperty), new PropertyMetadata(false));
public static bool GetValue(DependencyObject obj)
{
return (bool)obj.GetValue(ValueProperty);
}
public static void SetValue(DependencyObject obj, bool value)
{
obj.SetValue(ValueProperty, value);
}
}
The code is consumed as follows (here my code is not in use):
<Button Content="Login"
IsDefault="True"
local:BaseAttachedProperty`2.Value="{Binding LoginIsRunning}" <-- Here is the trouble area
Command="{Binding LoginCommand}"
CommandParameter="{Binding ElementName=Page}"
HorizontalAlignment="Center" />
If the attached property works, the correct code should be
<Button Content="Login"
IsDefault="True"
local:IsBusyProperty.Value="{Binding LoginIsRunning}"
Command="{Binding LoginCommand}"
CommandParameter="{Binding ElementName=Page}"
HorizontalAlignment="Center" />