locked
code works in XAMLPad, but not in Silverlight RRS feed

  • Question

  • I have the following code:

     

    1    <Grid Background="Transparent" Height="Auto" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center">
    2    	<Grid.RowDefinitions>
    3    		<RowDefinition Height="Auto"/>
    4    		<RowDefinition Height="Auto"/>
    5    	</Grid.RowDefinitions>
    6    	<Rectangle Grid.RowSpan="2" Fill="Yellow" RadiusX="15" RadiusY="15" Margin="4,0"/>
    7    	<Rectangle Fill="GreenYellow" RadiusX="5" RadiusY="5" MinHeight="15" MinWidth="15"/>
    8    	<Grid Name="HeaderGrid">
    9    		<Grid.ColumnDefinitions>
    10   			<ColumnDefinition />
    11   			<ColumnDefinition Width="{Binding ActualHeight, ElementName=LockButton}"/>
    12   		</Grid.ColumnDefinitions>
    13   		<TextBlock Text="Header" FontSize="40" Name="hdr" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"/>
    14   		<ToggleButton Grid.Column="1" Grid.Row="0" Name="LockButton"
    15   			VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsChecked="True" />
    16   	</Grid>
    17   </Grid>
    

     

    The problem is with line 11, namely the >>{Binding ActualHeight, ElementName=LockButton}<< part. What I was aiming for here was to have the button always a square. This code works perfectly in XAMLPad, but when I create a SL application with this, I get an exception.


    Code: 4004   
    Category: ManagedRuntimeError      
    Message: System.Windows.Markup.XamlParseException: System.Windows.Markup.XamlParseException: System.Windows.Markup.XamlParseException: System.Windows.Markup.XamlParseException: AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 11 Position: 33]
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight) [Line: 0 Position: 0]
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight) [Line: 0 Position: 0]
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight) [Line: 0 Position: 0]
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)

     Why is that?  How to fix it?

    Tuesday, February 2, 2010 6:38 PM

Answers

All replies

  • ActualWidth and AcutalHeight are read-only properties, which is why you are getting the errors.  You will need to set the Width and Height properties instead.

    Hope this helps.

    Tony

    Tuesday, February 2, 2010 10:05 PM
  • Width="{Binding ActualHeight, ElementName=LockButton}"

    It's assigning LockButton's ActualHeight TO Column's Width so it is valid (like Column.Width = LockButton.ActualHeight). As I said, it works in XAMLPad so the syntax is good. The problem is that Silverlight throws an exception.

    Any other ideas?

    Wednesday, February 3, 2010 12:56 PM
  • Sorry about that, I looked at it too quickly.  You cannot bind to the ColumnDefinition Width column because ColumnDefinition is not derived from a FrameElement (refer to this thread: http://forums.silverlight.net/forums/t/76992.aspx).

    Hope this helps.

    Tony

     

    Wednesday, February 3, 2010 3:19 PM
  • That's strange. If that's the issue then why does it work in XAMLPad just the way I wanted (resizing the text with fontsize resizes the button which remains a square thanks to this binding, and most importantly it doesn't throw an exception)? Does Silverlight differ from what renders stuff in XAMLPad?

    Wednesday, February 3, 2010 5:30 PM
  • There are some differences in XAML between WPF and Silverlight.  Some is due to Silverlight being a subset of WPF and others are due to differences in how XAML is implemented between the two.  The differences in implementation are a lot fewer now than there were, but they are still there.

     Tony

     

    Wednesday, February 3, 2010 6:20 PM