question

Sergey-8683 avatar image
0 Votes"
Sergey-8683 asked HuiLiu-MSFT commented

WPF: Resize the window to a button size

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


windows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT commented

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 to enable e-mail notifications if you want to receive the related email notification for this thread. 


2.gif (45.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@Sergey-8683.May I know if you have got any chance to check my answer? I am glad to help if you have any others questions. 

0 Votes 0 ·