Question

I've followed the instruction from Adding custom attributes to an element in XAML? but unfortunately the designer tells me that he can't find the element and when starting the program I get an XamlParserException with the message Cannot set unknown member '{clr-namespace:myNs}MediaElementProperties.MediaId'.

My Setup:

  • Xaml-Page that is loaded dynamically with the command XamlReader.Load(fileStream) for displaying
  • The content page itself which uses the code like this:

    <MediaElement myNs:MediaElementProperties.MediaId="test" ... />
    

    where myNs was defined with

     xmlns:myNs="clr-namespace:MyNamespace"
    
  • And the Definition of the MediaElementProperties which looks like this:

    namespace MyNamespace {
    public static class MediaElementProperties
    {
        public static readonly DependencyProperty MediaIdProperty = 
           DependencyProperty.Register("MediaId", typeof(string), typeof(MediaElementProperties), new FrameworkPropertyMetadata(string.Empty));
    
        public static string GetMediaId(UIElement element)
        {
            return (string)element.GetValue(MediaIdProperty);
        }
    
        public static void SetMediaId(UIElement element, string value)
        {
            element.SetValue(MediaIdProperty, value);
        }
    }}
    

Do you have any ideas why I keep getting the exception?

Was it helpful?

Solution

Attached properties need to be registered with RegisterAttached as noted by Zabavsky.

When using the XamlReader you may need to be required to fully qualify your xmlns, even though the code is in the same assembly, i.e.

xmlns:myNs="clr-namespace:MyNamespace;assembly=MyApplication"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top