NotSure
NotSure

Reputation: 681

Removing full screen in xaml dynamically

This will probably be very simple for most of you, I am new to XAML and WPF. I have an app that startes att full screen, I did this by adding

  WindowState="Maximized"
  WindowStyle="None"

I want to have a button that simply eliminates this part. I have a "Full screen" button in the xaml and by click he is calling a "FullScreen_Click" function in my code. I just need to know what to write in the code that will eliminate the full screen if it is on full screen mode and restore it to full screen when it is not.

Upvotes: 0

Views: 203

Answers (2)

Chris W.
Chris W.

Reputation: 23280

For a xaml only technique just for reference to see a xaml example in comparison (but I would do @mm8's route, it's simpler);

1. Bind your property to that of another like: 

<Window WindowState="{Binding Tag, ElementName=toggleState}" .... />

2. Use a `ToggleButton` or similar control and `Triggers`

.

<!-- like this PoC -->
    <Grid>
        <Grid.Resources>
            <Style x:Key="cwWindowState_PoC" TargetType="{x:Type ToggleButton}">
                <Setter Property="Tag" Value="Maximized"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToggleButton">
                            <Grid>
                                <Border Background="{TemplateBinding Background}"/>
                                <ContentPresenter x:Name="MyContentPresenter" 
                                                  Content="{TemplateBinding Tag}" 
                                                  HorizontalAlignment="Center" 
                                                  VerticalAlignment="Center" />

                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Tag" Value="Normal" />
                                </Trigger>
                                <Trigger Property="IsChecked" Value="False">
                                    <Setter Property="Tag" Value="Maximized" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>

            <ToggleButton x:Name="toggleState" Content="Click Me" 
                          Background="Green"
                          Style="{StaticResource cwWindowState_PoC}"/>

    </Grid>

Could also use DataTrigger but that requires interaction triggers instead of just a property setter from a template.

Upvotes: 1

mm8
mm8

Reputation: 169270

Try this:

private void FullScreen_Click(object sender, RoutedEventArgs e)
{
    WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}

This will toggle between WindowState.Maximized and WindowState.Normal each time the Button is clicked.

Upvotes: 3

Related Questions