Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SL4 TransformToVisual: "Value does not fall within the expected range."

I have a ScrollViewer containing a Canvas:

<ScrollViewer x:Name="scrollViewer" Margin="0" Grid.RowSpan="2" >
<i:Interaction.Triggers>
<!--    stuff-->
</i:Interaction.Triggers>
<Border>
    <Canvas x:Name="MapCanvas" Background="{Binding BackgroundColor}" Width="7200" Height="3200" >
        <Canvas.RenderTransform>
            <ScaleTransform />
        </Canvas.RenderTransform>
        <i:Interaction.Behaviors>
        <!--            stuff-->
        </i:Interaction.Behaviors>

        <i:Interaction.Triggers>
        <!--            stuff-->
        </i:Interaction.Triggers>
    </Canvas>
</Border>

This code causes an exception, "Value does not fall within the expected range."

var generalTransform = scrollViewer.TransformToVisual(MapCanvas);     

Any hints on what causes this would be appreciated. Looking in the online help, I couldn't find documentation of what exceptions TransformToVisual might throw or why.

UPDATE: Is there q way to test the scrollViewer and/or MapCanvas to make sure the TransformToVisual call will succeed?

like image 970
Number8 Avatar asked Nov 15 '22 00:11

Number8


1 Answers

Maybe I am completely wrong but I suspect this could be a timing issue.

If I do

private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 0, 0, 20);
    timer.Tick += (s, e2) =>
    {
        var generalTransform = scrollViewer.TransformToVisual(MapCanvas);
        timer.Stop();
    };
    timer.Start();
}

the error doesn't occur anymore...

I guess it's because the controls haven't been 'visually' shown on the screen?

like image 154
Justin XL Avatar answered Dec 08 '22 00:12

Justin XL