Checking the value of the window's "WindowState" in a Trigger
Asked Answered
T

3

6

In WPF, is there a way to check the window's "WindowState" property in a Trigger? I've tried using the value of "0", "Minimized" and "WindowState.Minimized."

EXAMPLE:

<Window.Triggers>
    <Trigger Property="WindowState" Value="Minimized">
        <Setter Property="ShowInTaskBar" Value="False" />
    </Trigger>
</Window.Triggers>
Tasia answered 12/1, 2011 at 16:3 Comment(0)
P
8

Works like this:

<Window.Style>
    <Style TargetType="Window">
        <Style.Triggers>
            <Trigger Property="WindowState" Value="Minimized">
                <Setter Property="ShowInTaskbar" Value="False" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Style>

Edit: You need to place your trigger in the Window.Style.

Privatdocent answered 12/1, 2011 at 16:29 Comment(1)
Thanks, lifesaver! I used this to increase the border size slightly to avoid the border disappearing when the window is maximised.Gene
R
10

Or if you want a control other than the window to respond to the WindowState property you can use a DataTrigger instead:

<DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" 
             Value="Normal">
   <Setter Property="Fill" Value="Green"/>
</DataTrigger>
Riedel answered 6/7, 2017 at 10:12 Comment(0)
P
8

Works like this:

<Window.Style>
    <Style TargetType="Window">
        <Style.Triggers>
            <Trigger Property="WindowState" Value="Minimized">
                <Setter Property="ShowInTaskbar" Value="False" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Style>

Edit: You need to place your trigger in the Window.Style.

Privatdocent answered 12/1, 2011 at 16:29 Comment(1)
Thanks, lifesaver! I used this to increase the border size slightly to avoid the border disappearing when the window is maximised.Gene
G
0

Example of how to increase border thickness when a window is maximised. Otherwise, due to oddities of WindowChrome, the border will disappear.

This example also strips out the standard window header, so you have to add your own minimize/maximize/close buttons.

<Window ResizeMode="CanResizeWithGrip"
        WindowStyle="SingleBorderWindow">
    <!-- Remove window header and border. Use with ResizeMode="CanResizeWithGrip" and WindowStyle="SingleBorderWindow". -->
    <WindowChrome.WindowChrome>
        <WindowChrome     
            CaptionHeight="1"  
            CornerRadius ="0"
            ResizeBorderThickness="4"         
            GlassFrameThickness="0">
        </WindowChrome>
    </WindowChrome.WindowChrome>            
    <Border BorderThickness="1">     
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <!-- Add to avoid border disappearing when window is maximised -->
                    <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" 
                                 Value="Maximized">
                        <Setter Property="Margin" Value="10"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" 
                                 Value="Normal">
                        <Setter Property="Margin" Value="0"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Grid>
           <!-- Window XAML here. -->
        <Grid>
     </Border>
 </Window>
Gene answered 19/2, 2019 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.