locked
Silverlight WP7 bug AG_E_PARSER_BAD_PROPERTY_VALUE RRS feed

  • Question

  • I am convinced this must be a bug, since all reasonable explanations for why this happens fail me.

     

    I have an image that exists within an ItemControl.ItemTemplate

     

    <ItemsControl x:Name="ItemsControl">
    	<ItemsControl.ItemTemplate>
    		<DataTemplate>
    			<Image Source="{Binding Data.ImageSource}">
    				<Image.Projection>
    					<PlaneProjection RotationX="45" CenterOfRotationX="{Binding WTF}" />
    				</Image.Projection>
    			</Image>
    			<TextBox Text="{Binding WTF}" />
    		</DataTemplate>
    	</ItemsControl.ItemTemplate>
    </ItemsControl>


    WTF is a property in an ordinary class

    public class AnimatedTile// : DependencyObject
    {
        public AnimatedTileData Data { get; set; }
    
        public double WTF { get { return 0.5; } }
    
        public AnimatedTile()
        {
        }
    
        public AnimatedTile(AnimatedTileData data)
            : this()
        {
            this.Data = data;
        }
    }


     

    I don't get an error when I bind WTF to anything except for PlaneProjection fields. Why is this?

     

    Here is the error I get:

    AG_E_PARSER_BAD_PROPERTY_VALUE

    Could not evaluate expression

     

    Thursday, August 19, 2010 11:11 AM

Answers

  • Hi Ace,

    Yeah, this one's a bit of an inconvenience. WP7 runs on Silverlight 3, and in Silverlight 3, you can only use binding with an element that inherits from FrameworkElement (unlike WPF and Silverlight 4, where the binding root is DependencyObject). In this case, as PlaneProjection inherits from DependencyObject and not FrameworkElement, you can't bind to it's properties.

    Bummer.

    However, you can work around this by writing an attached property that takes the centre of rotation you require (and you can bind to this if you put it on the Image, not the projection) and then sets the projection accordingly.

    Regards,
    Rob

    EDIT: I'll have a stab at knocking up an attached property for you shortly and post it here.

    Friday, August 27, 2010 7:18 AM

All replies

  • Hi Ace,

    Yeah, this one's a bit of an inconvenience. WP7 runs on Silverlight 3, and in Silverlight 3, you can only use binding with an element that inherits from FrameworkElement (unlike WPF and Silverlight 4, where the binding root is DependencyObject). In this case, as PlaneProjection inherits from DependencyObject and not FrameworkElement, you can't bind to it's properties.

    Bummer.

    However, you can work around this by writing an attached property that takes the centre of rotation you require (and you can bind to this if you put it on the Image, not the projection) and then sets the projection accordingly.

    Regards,
    Rob

    EDIT: I'll have a stab at knocking up an attached property for you shortly and post it here.

    Friday, August 27, 2010 7:18 AM
  • Here you go - tested and working!

    using System.Windows;
    using System.Windows.Media;
    
    namespace NXG.Controls
    {
    	public class CenterOfRotationXBindingHelper
    	{
    
    		#region CenterOfRotationX (Attached DependencyProperty)
    
    		public static readonly DependencyProperty CenterOfRotationXProperty =
    			DependencyProperty.RegisterAttached("CenterOfRotationX",
    			typeof(double),
    			typeof(CenterOfRotationXBindingHelper),
    			new PropertyMetadata(0.0, new PropertyChangedCallback(OnCenterOfRotationXChanged)));
    
    		public static void SetCenterOfRotationX(DependencyObject o, double value)
    		{
    			o.SetValue(CenterOfRotationXProperty, value);
    		}
    
    		public static double GetCenterOfRotationX(DependencyObject o)
    		{
    			return (double)o.GetValue(CenterOfRotationXProperty);
    		}
    
    		private static void OnCenterOfRotationXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    		{
    			FrameworkElement element = d as FrameworkElement;
    			if (element != null)
    			{
    				PlaneProjection projection = element.Projection as PlaneProjection;
    				if (projection != null)
    				{
    					projection.CenterOfRotationX = (double)e.NewValue;
    				}
    			}
    		}
    
    		#endregion
    
    	}
    }
    


    Use the following XAML to use it (don't forget declare the local namespace as your assembly):

    <Image Source="{Binding Data.ImageSource}" local:CenterOfRotationXBindingHelper.CenterOfRotationX="{Binding WTF}">

     

    Regards,
    Rob 

    Friday, August 27, 2010 8:03 AM
  • Thanks Bob

     

    That's really helpful! 

    Wednesday, September 8, 2010 12:02 PM