none
Bind to custom attached property from code results in catastrofic failure

    Question

  • I'm using a custom attached property, i.e. MyCanvas.Left. From XAML it's possible to create a binding to using the correct prefix, however from code behind this gives a catastofic failure. The reason seems to be only core attached properties can be attached to from code.

    In code:

    PropertyPath path = new PropertyPath(MyCanvas.Left); // No error, use any custom attached property
    Binding binding = new Binding();
    binding.Path = path; // Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

    Is there a workaround available?

    Leon

    Thursday, December 31, 2009 11:40 AM

Answers

  • In case anyone comes across this thread - I've just done this in my code and it works fine in SL5 RTM.

    Tuesday, March 13, 2012 7:14 PM

All replies

  •  Set the property path as string.

                PropertyPath path = new PropertyPath("Canvas.LeftProperty");
    Binding binding = new Binding();
    binding.Path = path;
      Hope this helps.
    Friday, January 01, 2010 12:07 AM
  •  I have been working on a similar issue.  I tried for hours to set an custom attached property binding in code, but continually received an error.  I was having similar problems with Expression Blend.  Although setting the attached bindings in XAML worked fine when executed, it produced exceptions in Blend that became a real nuisance.

    My solution is a hack, but it works.  You can use the System.Windows.Markup.XamlReader to read in a binding string with the correct namespace definitions and binding path:

    System.Windows.Data.Binding b = System.Windows.Markup.XamlReader.Load("<Binding xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Path=\"(MyCanvas.Left)\" />");

     From there you can set any other properties (RelativeSource, Mode, etc.) on the Binding object in code.  Note that if your MyCanvas.Left property is in a different assembly, you will need to add an xmlns entry and use the format (AssemblyName:MyCanvas.Left) in the string you pass to the XamlReader.

    In the end, I wrote my own class for setting Custom Attached Property bindings.  You might find some snippets you can use in it:   http://forums.silverlight.net/forums/t/153280.aspx.  Good luck.  I hope we get some info about whether it will be easier to bind Attached Properties in SL4!

    Friday, January 01, 2010 2:51 AM
  • I'm not sure what you try to bind and what you want to do. But the PropertyPath is the name of the property, for example "Path". the Source property of the Binding class is the Source.. So something like this will work:

     

           <Canvas x:Name="MyCanvas" Canvas.Left="100">
                <TextBox Text="Hello" x:Name="myText"/>
            </Canvas>
      
                PropertyPath path = new PropertyPath("Left");
                Binding binding = new Binding();
                binding.Path = path;
                binding.Source = MyCanvas;
    
                myText.SetBinding(TextBox.HeightProperty, binding);
     The above code will get the Left property value from the MyCanvas and bind it to the TextBox.Height property..
    Friday, January 01, 2010 4:54 AM
  • The code you suggest works; Canvas.Left is a core attached property (in that case, my first example also works). The problem is when you try to attach to a custom attached property (one you've created yourself). In that case both versions give you the same error.
    Friday, January 01, 2010 5:12 AM
  • My solution is a hack, but it works.  You can use the System.Windows.Markup.XamlReader to read in a binding string with the correct namespace definitions and binding path:


    Great! I should have thought about that. Thanks for the hack :D

    I decided not to mark your reply as an answer; although it's a great workaround, and I'm going to use it, I still feel this is a bug in silverlight which should be fixed...

    Friday, January 01, 2010 5:16 AM
  •  The above code will get the Left property value from the MyCanvas and bind it to the TextBox.Height property..

    Your code works with a core dependency property, I'm looking for binding to a custom attached property. In XAML it would be (cc is the namespace for the custom code):

           <cc:MyCustomCanvas x:Name="MyCanvas">
                <TextBox Text="Hello" cc:MyCustomCanvas.Left="100" x:Name="myText"/>
            </cc:MyCustomCanvas>

    And then try to bind the cc:MyCustomCanvas.Left attached property...

    Friday, January 01, 2010 5:26 AM
  • Oh, sorry missed that part.. have you looked at this article, isn't that the same thing you try to do?

    http://geekswithblogs.net/NewThingsILearned/archive/2008/01/15/binding-to-an-attached-property.aspx

    Friday, January 01, 2010 5:50 AM
  • Oh, sorry missed that part.. have you looked at this article, isn't that the same thing you try to do?

    http://geekswithblogs.net/NewThingsILearned/archive/2008/01/15/binding-to-an-attached-property.aspx


    Yes, thats exactly what I want to do. I see in WPF it works without problems, now just add that fix to Silverlight ;)

    Friday, January 01, 2010 6:31 AM
  • leon77:

    I decided not to mark your reply as an answer; although it's a great workaround, and I'm going to use it, I still feel this is a bug in silverlight which should be fixed...

      I agree that this is a bug.  Does anyone know if this is resolved in SL4 yet?

    Friday, January 01, 2010 1:30 PM
  •   I agree that this is a bug.  Does anyone know if this is resolved in SL4 yet?


    I'm using silverlight 4 beta, it's not fixed.... Maybe in the RC version. In WPF the same code works without problems.

    Friday, January 01, 2010 2:46 PM
  • Unfortunately, this bug was not resolved.

    Friday, September 17, 2010 2:53 PM
  • Looks like it's getting fixed in SL 5

    Thursday, June 30, 2011 9:13 PM
  • In case anyone comes across this thread - I've just done this in my code and it works fine in SL5 RTM.

    Tuesday, March 13, 2012 7:14 PM