Asked by:
Binding to attached property not working

Question
-
It appears that attached properties do not work on bindings in WinRT, yet I cannot find any documentation specifying that this is the case. One would expect it to be supported, because it is supported in animations the documentation does state that: "For example, the string to specify that you want to animate the Grid.Row attached property on an object, use the property path "(Grid.Row)"." (http://msdn.microsoft.com/en-us/library/jj569302.aspx). There is an issue in this forum suggesting that it didn't and then that it did:
http://social.msdn.microsoft.com/Forums/windowsapps/en-US/69309983-22d6-484a-99ae-c05b22c9d73e/unable-to-bind-to-an-attached-propertyBut I can't get it to work. In xaml (using the same code as in the previous article):
<TextBlock Text="{Binding (meta:MyMetadata.Title)}"/>
and:
public class MyMetadata:DependencyObject { public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached("Title", typeof(string), typeof(MyMetadata), null); public static void SetTitle(DependencyObject obj, string propertyValue) { obj.SetValue(TitleProperty, propertyValue); } public static string GetTitle(DependencyObject obj) { return obj.GetValue(TitleProperty) as string; } }
At run time I get this error (interestingly this is the same error I was getting last week at compile time, but a different situation. I wasn't aware it was related to attached properties at the time):
WinRT information: Failed to assign to property 'Windows.UI.Xaml.Data.Binding.Path'. [Line: 14 Position: 16]So I'm not clear how to use an attached property in a binding? This same code works perfectly fine in WPF and Silverlight.
Stefan
Saturday, May 10, 2014 9:22 PM
All replies
-
Hi Stefan,
where does the value come from? From an DependencyObject in the DataContext that has the Attached-Property set?
I just tried as a test this:
<TextBlock FontSize="40" local:MyMetadata.Title="Just a test" Text="{Binding RelativeSource={RelativeSource Self}, Path=(local:MyMetadata.Title)}"/>
and it works fine.
Thomas
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest app: The "Womanizer" :-)
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbookSunday, May 11, 2014 8:41 AM -
In my actual case the property actually gets set later, and is actually relativesource self. If I use the line that you have without setting the property:
<TextBlock FontSize="40" Text="{Binding RelativeSource={RelativeSource Self}, Path=(meta:MyMetadata.Title)}"/>
Then it fails.
So the bug in windows is that it should be able to handle the value turning up later (plus there is no decent description of the error). There are scenarios where I wanted to use it where the value may not be set at the time of initialisation, because the datacontext accepted hasn't been set up yet.
But at least it's clear where it does and doesn't work, so hopefully that will help others as well.
I'd be good to hear from the Windows team why it works this way and whether it will be fixed for future releases?
Stefan
Sunday, May 11, 2014 9:04 PM -
This is strange I just checked it works fine on my application. Please check my code.
Best regards and goodluck
public static bool GetObserve(FrameworkElement elem) { return (bool)elem.GetValue(ObserveProperty); } public static void SetObserve(FrameworkElement elem, bool value) { elem.SetValue(ObserveProperty, value); } public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached("Observe", typeof(bool), typeof(MouseObserver), new PropertyMetadata(false,OnObserveChanged));
Agha Khan
Sunday, May 11, 2014 11:30 PM