0

I have an image of a map inside a scrollviewer and, using 2 canvas, I place a little square (or a map pin image) after the user selects where they are.

Now, since they do not "click" on the map to do so, but they select their location from a list, I need to make that square the center of the view, in the scrollviewer, once they see the map.

In my xaml, I have just a few things since, all the information, comes from an API

<Grid x:Name="firstFloor" Margin="0,50,0,0" Visibility="Visible">
            <ScrollViewer x:Name="pruebaScrollViewer1" BringIntoViewOnFocusChange="True" ZoomMode="Enabled" HorizontalScrollBarVisibility="Auto" HorizontalScrollMode="Enabled">
                <Grid x:Name="myMap1stFloor">
                </Grid>
            </ScrollViewer>
</Grid>

and in code behind I have

private void showStorePosition2(string map_id, string name, int width, int height, int tile, int x, int y)
{
        IdOfMapWhereIAm = map_id;

        if (map_id == "10")
        {
            if (!_primerPiso)
            {
                //my parent canvas is defined globally
                //the width and height of it come from API
                myParentCanvas.Width = width;
                myParentCanvas.Height = height;
                myParentCanvas.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
                myParentCanvas.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
                Debug.WriteLine(width);

                ImageBrush pushpin = new ImageBrush();
                pushpin.ImageSource = new BitmapImage(new Uri("ms-appx:/Imagenes/mappin.png", UriKind.Absolute));                    
                TextBlock storeName = new TextBlock();
                storeName.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
                storeName.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
                storeName.TextAlignment = TextAlignment.Center;
                storeName.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Open Sans");
                storeName.Height = 20;
                storeName.Width = Double.NaN;
                storeName.FontSize = 15;
                storeName.Text = name;
                storeName.Margin = new Thickness(-20, -20, 0, 0);
                storeName.TextWrapping = TextWrapping.Wrap;
                storeName.Foreground = new SolidColorBrush(Colors.Black);

                Canvas myInititialStore = new Canvas();                    
                myInititialStore.Background = pushpin;
                myInititialStore.Height = 13;
                myInititialStore.Width = 13;
                Canvas.SetTop(myInititialStore, (y) * tile);
                Canvas.SetLeft(myInititialStore, (x) * tile);
                myInititialStore.Children.Add(storeName);

                myParentCanvas.Children.Add(myInititialStore);
                myMap1stFloor.Children.Add(myParentCanvas);
            }
        }
}

I was thinking about giving myInitialStore a dependency property, setting the focusProperty, but I do not know how to do it either.

Community
  • 1
  • 1
Gustavo
  • 33
  • 6

1 Answers1

0

In case someone has the same problem I had, I found a way to "focus" on the Canvas I needed to:

Instead of trying to do so, I changed the view in the scrollviewer, using the Canvas values, that way, when the Canvas is loaded inside the scrollviewer, this last one moves automatically to where the Canvas is located:

ScrollViewer1.ChangeView(((initialStore_x * tileMapToGo) - (widthMapToGo / tileMapToGo)), ((initialStore_y * tileMapToGo) - (heightMapToGo / tileMapToGo)), 1, false);

Hope it helps someone. Regards

Gustavo
  • 33
  • 6