WPF: Resize the window to a button size

Sergey 1 Reputation point
2021-08-31T08:42:47.73+00:00

Hello, i want be able to resize the whole window to a first button size on that button click. And restore it's original size on next click. I hope that there are some implementations of this kind of functionality, but didn't find it yet.
Could someone share an example or describe the plan how to manage it nicely.

I thought that i should dinamicaly create all other subcontrols and remove them from the grid on button click, to be able to shrink window size to first button only.
Or maybe i can move all controls under the first button and make them invisible, so that only one that button was shown.
Thanks a lot.

127881-2021-08-31-11-32-23.png

Developer technologies Windows Presentation Foundation
Developer technologies XAML
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-09-01T08:39:30.24+00:00

    You could use ToggleButton to change the window size and restore the size. For displaying Button, HorizontalAlignment and VerticalAlignment in Grid default to Center. And you could edit other controls first in the code and edit Button last. You can try to refer to the following code.
    The code of xaml:

    <Window x:Class="WindowSizeAndLocation.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WindowSizeAndLocation"  
            mc:Ignorable="d" Name="window" WindowStyle="None"   
            Title="Window" Height="450" Width="450">  
        <Grid Name="grid">  
            <TextBlock Height="262" Width="100" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Cornsilk">Growth Complete!</TextBlock>  
            <TextBlock VerticalAlignment="Top" HorizontalAlignment="Center" TextAlignment="Center" Width="100"> hello</TextBlock>  
            <ToggleButton Width="100" Height="30" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked">size</ToggleButton>  
        </Grid>  
    </Window>  
    

    The code of xaml.cs:

    private void ToggleButton_Checked(object sender, RoutedEventArgs e)  
        {  
          Control control = (Control)sender;  
          this.window.Width = control.Width;  
          this.window.Height = control.Height +20;  
        }  
    
        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)  
        {  
          this.window.Width = 450;  
          this.window.Height = 450;  
    }  
    

    The picture of result:

    128209-2.gif


    If the response is helpful, please click "Accept Answer" and upvote it. 
    Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [3]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.