How to make all controls resize accordingly proportionally when window is maximized?

后端 未结 3 1950
野的像风
野的像风 2020-11-29 17:05

When I clicked on the maximize button the window is maximized but the controls are not resized proportionally. What is the best way to make the controls resize accordingly?

3条回答
  •  渐次进展
    2020-11-29 17:44

    Just thought i'd share this with anyone who needs more clarity on how to achieve this:

    myCanvas is a Canvas control and Parent to all other controllers. This code works to neatly resize to any resolution from 1366 x 768 upward. Tested up to 4k resolution 4096 x 2160

    Take note of all the MainWindow property settings (WindowStartupLocation, SizeToContent and WindowState) - important for this to work correctly - WindowState for my user case requirement was Maximized

    xaml

    
      
        
            
    
            
            
            
                
                    
                        
                        
                        
                        
                    
                
            
    

    .cs

     private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            myCanvas.Width = e.NewSize.Width;
            myCanvas.Height = e.NewSize.Height;
    
            double xChange = 1, yChange = 1;
    
            if (e.PreviousSize.Width != 0)
                xChange = (e.NewSize.Width / e.PreviousSize.Width);
    
            if (e.PreviousSize.Height != 0)
                yChange = (e.NewSize.Height / e.PreviousSize.Height);
    
            ScaleTransform scale = new ScaleTransform(myCanvas.LayoutTransform.Value.M11 * xChange, myCanvas.LayoutTransform.Value.M22 * yChange);
            myCanvas.LayoutTransform = scale;
            myCanvas.UpdateLayout();
        }
    

提交回复
热议问题