0

i'm struggling with setting attached properties out of a different project in a storyboard with xaml.

In the style of a textbox i have an eventtrigger which should set an attached property to true after an event.

XAML:

xmlns:MyNamespace="clr-namespace:Project.Utilities"

<EventTrigger RoutedEvent="SomeEvent">
   <BeginStoryboard>
      <Storyboard>
         <BooleanAnimationUsingKeyFrames
            Storyboard.TargetProperty="MyNamespace:MyClass.MyAttachedProperty">
            <DiscreteBooleanKeyFrame Value="True" />
         </BooleanAnimationUsingKeyFrames>
      </Storyboard>
   </BeginStoryboard>
</EventTrigger>

C#:

namespace MyNamespace
{
   public static class MyClass
   {
      public static readonly DependencyProperty MyAttachedPropertyProperty
         = DependencyProperty.RegisterAttached("MyAttachedProperty", typeof(bool), 
           typeof(MyClass), new UIPropertyMetadata(false));

      public static void SetMyAttachedProperty(DependencyObject target, bool value)
      {
        target.SetValue(MyAttachedPropertyProperty, value);
      }

      public static bool GetMyAttachedProperty(DependencyObject target)
      {
        return (bool)target.GetValue(MyAttachedPropertyProperty);
      }
   }
}

With the PropertyPath I did it like discribed in here. But I always get an 'System.InvalidOperationException' with: Additional information: Cannot resolve all property references in the property path 'MyNamespace:MyClass.MyAttachedProperty'. Verify that applicable objects support the properties.

I tried several different ways to write the path with parenthesise and without them but honestly I don't quite get it how the path has to be defined it even not with the help of the MSDN Post

Does anyone has an idea what else I could try to access the attached property?

Thanks in advance!

Community
  • 1
  • 1
e_card
  • 119
  • 2
  • 10

1 Answers1

2

From the Attached Property section of the English version of the page that you linked to, Property Path Syntax:

Attached Property

<object Path="...(ownerType.propertyName)..." ... />

The parentheses indicate that this property in a PropertyPath should be constructed using a partial qualification because it needs late-binding context information, such as the type being applied to for applied templates. It can use a prefix usage for a XAML namespace to find the type. The ownerType searches types that a XAML processor has access to, based on xmlns mappings (or using the default XAML namespace if unqualified). propertyName must resolve to the name of a property that exists on the ownerType.

I have highlighted a section that says that you can use a prefix, so that would turn the example into this:

<object Path="...(namespacePrefix:ownerType.propertyName)..." ... />

Further on at the bottom of the linked page, there is a community addition from Raphael Schweizer:

Property Path for Custom DP in Animation does not work

It should be noted that the Property Path syntax for attached properties works only with the MS attached properties but not for Custom attached properties.

Answer

Custom attached properties can be used

• XAMLPath = (Namespace:CustomOwner.AttachedProperty)

If you tried this and it didn't work, then you have a problem elsewhere in your code.

Sheridan
  • 68,826
  • 24
  • 143
  • 183