locked
Get all images on stages

    Question

  • Hi, I want to change all images coordinates and dimensions dynamically. 

    private void setResolution()
        {
            var bounds = Window.Current.Bounds;
            ratioHeight = bounds.Height / standartHeight;
            ratioWidth = bounds.Width / standartWidth;
    
            Canvas.SetLeft(img1, Canvas.GetLeft(img1) * (ratioWidth));
            Canvas.SetTop(img1, Canvas.GetTop(img1) * (ratioHeight));
            img1.Width = img1.ActualWidth * (ratioWidth);
            img1.Height = img1.ActualHeight * (ratioHeight);
        }

    It worked with these code but i have 17 pages and about 500 pics. i guess i need something like foreach but i dont know how.



    • Edited by Murat VAROL Thursday, March 6, 2014 3:56 PM
    Thursday, March 6, 2014 11:18 AM

Answers

  • Hi Murat,

    Two ideas in my mind.

    #1, use VisualTreeHelper get all the Image control and apply the new width and height to them.

    #2, use Binding with Convert, new width = oldWidth*ratioWidth

    Hope helps.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, March 6, 2014 12:23 PM
    Moderator

  • foreach (Image Images in FindVisualChildren<Image>(pageMain)) { Canvas.SetLeft(Images, Canvas.GetLeft(Images) * (ratioWidth)); Canvas.SetTop(Images, Canvas.GetTop(Images) * (ratioHeight)); Images.Width = Images.ActualWidth * (ratioWidth); Images.Height = Images.ActualHeight * (ratioHeight); }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            yield return (T)child;
                        }
    
                        foreach (T childOfChild in FindVisualChildren<T>(child))
                        {
                            yield return childOfChild;
                        }
                    }
                }
            }

    i wrote these codes and it worked. (pageMain is Page's name)


    Thursday, March 6, 2014 4:43 PM

All replies

  • Hi Murat,

    Two ideas in my mind.

    #1, use VisualTreeHelper get all the Image control and apply the new width and height to them.

    #2, use Binding with Convert, new width = oldWidth*ratioWidth

    Hope helps.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, March 6, 2014 12:23 PM
    Moderator

  • foreach (Image Images in FindVisualChildren<Image>(pageMain)) { Canvas.SetLeft(Images, Canvas.GetLeft(Images) * (ratioWidth)); Canvas.SetTop(Images, Canvas.GetTop(Images) * (ratioHeight)); Images.Width = Images.ActualWidth * (ratioWidth); Images.Height = Images.ActualHeight * (ratioHeight); }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            yield return (T)child;
                        }
    
                        foreach (T childOfChild in FindVisualChildren<T>(child))
                        {
                            yield return childOfChild;
                        }
                    }
                }
            }

    i wrote these codes and it worked. (pageMain is Page's name)


    Thursday, March 6, 2014 4:43 PM