none
Maximize window with SizeToContent=WidthAndHeight RRS feed

  • Question

  • Hello,
    how can I maximize the window from code when its SizeToContent property is set to WidthAndHeight?

    WindowState = WindowState.Maximized does not work - simple repro:
     
    <Window x:Class="WpfApplication1.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" SizeToContent="WidthAndHeight">  
        <Button Click="Button_Click">Maximize</Button>  
    </Window>  
     
    private void Button_Click(object sender, RoutedEventArgs e)  
    {  
        WindowState = WindowState.Maximized;  
    }  
     


    Run it and double-click the title bar. The window is maximized. Double-click it to restore bounds. Now press the button. The window gets positioned and themed as maximized one, but the size does not change.

    So how to maximize it really? Any command or some other trick?

    Jan
    Friday, September 5, 2008 10:35 PM

Answers

  • You will need to temporarily set the SizeToContent property to Manual before setting the WindowState to Maximized.  You can return it to WidthAndHeight when the window is restored.

    Below is a XamlPad-ready sample that demonstrates this approach:

     
    <Window   
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        Title="Window1" SizeToContent="WidthAndHeight">  
      <Window.Triggers> 
        <EventTrigger RoutedEvent="ToggleButton.Checked">  
          <EventTrigger.Actions> 
            <BeginStoryboard> 
              <Storyboard> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="SizeToContent">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static SizeToContent.Manual}" /> 
                </ObjectAnimationUsingKeyFrames> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="WindowState">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static WindowState.Maximized}" /> 
                </ObjectAnimationUsingKeyFrames> 
              </Storyboard> 
            </BeginStoryboard> 
          </EventTrigger.Actions> 
        </EventTrigger> 
        <EventTrigger RoutedEvent="ToggleButton.Unchecked">  
          <EventTrigger.Actions> 
            <BeginStoryboard> 
              <Storyboard> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="SizeToContent">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static SizeToContent.WidthAndHeight}" /> 
                </ObjectAnimationUsingKeyFrames> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="WindowState">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static WindowState.Normal}" /> 
                </ObjectAnimationUsingKeyFrames> 
              </Storyboard> 
            </BeginStoryboard> 
          </EventTrigger.Actions> 
        </EventTrigger> 
      </Window.Triggers> 
      <Grid HorizontalAlignment="Left" VerticalAlignment="Top">  
        <ToggleButton> 
          <ToggleButton.Style> 
            <Style TargetType="{x:Type ToggleButton}">  
              <Setter Property="Content" Value="Maximize" /> 
              <Style.Triggers> 
                <Trigger Property="IsChecked" Value="True">  
                  <Setter Property="Content" Value="Restore" /> 
                </Trigger> 
              </Style.Triggers> 
            </Style> 
          </ToggleButton.Style> 
        </ToggleButton> 
      </Grid> 
    </Window>   
     



    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Marked as answer by Jan Kučera Saturday, September 6, 2008 7:38 AM
    Saturday, September 6, 2008 6:07 AM

All replies

  • You will need to temporarily set the SizeToContent property to Manual before setting the WindowState to Maximized.  You can return it to WidthAndHeight when the window is restored.

    Below is a XamlPad-ready sample that demonstrates this approach:

     
    <Window   
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        Title="Window1" SizeToContent="WidthAndHeight">  
      <Window.Triggers> 
        <EventTrigger RoutedEvent="ToggleButton.Checked">  
          <EventTrigger.Actions> 
            <BeginStoryboard> 
              <Storyboard> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="SizeToContent">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static SizeToContent.Manual}" /> 
                </ObjectAnimationUsingKeyFrames> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="WindowState">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static WindowState.Maximized}" /> 
                </ObjectAnimationUsingKeyFrames> 
              </Storyboard> 
            </BeginStoryboard> 
          </EventTrigger.Actions> 
        </EventTrigger> 
        <EventTrigger RoutedEvent="ToggleButton.Unchecked">  
          <EventTrigger.Actions> 
            <BeginStoryboard> 
              <Storyboard> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="SizeToContent">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static SizeToContent.WidthAndHeight}" /> 
                </ObjectAnimationUsingKeyFrames> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="WindowState">  
                  <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static WindowState.Normal}" /> 
                </ObjectAnimationUsingKeyFrames> 
              </Storyboard> 
            </BeginStoryboard> 
          </EventTrigger.Actions> 
        </EventTrigger> 
      </Window.Triggers> 
      <Grid HorizontalAlignment="Left" VerticalAlignment="Top">  
        <ToggleButton> 
          <ToggleButton.Style> 
            <Style TargetType="{x:Type ToggleButton}">  
              <Setter Property="Content" Value="Maximize" /> 
              <Style.Triggers> 
                <Trigger Property="IsChecked" Value="True">  
                  <Setter Property="Content" Value="Restore" /> 
                </Trigger> 
              </Style.Triggers> 
            </Style> 
          </ToggleButton.Style> 
        </ToggleButton> 
      </Grid> 
    </Window>   
     



    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Marked as answer by Jan Kučera Saturday, September 6, 2008 7:38 AM
    Saturday, September 6, 2008 6:07 AM
  • Hi Dr. WPF.
    Thank you for the tip. Isn't it bug that both ways which should maximalize the window result into different states?

    Jan
    Saturday, September 6, 2008 7:38 AM