检查窗口的“WindowState”值在触发器中

发布于 2024-10-12 06:39:37 字数 365 浏览 3 评论 0原文

在WPF中,有没有办法在触发器中检查窗口的“WindowState”属性?我尝试过使用“0”、“Minimized”和“WindowState.Minimized”的值。

例子:

<Window.Triggers>
    <Trigger Property="WindowState" Value="Minimized">
        <Setter Property="ShowInTaskBar" Value="False" />
    </Trigger>
</Window.Triggers>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热血少△年 2024-10-19 06:39:37

或者,如果您希望窗口以外的控件响应 WindowState 属性,您可以使用 DataTrigger:

<DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" 
             Value="Normal">
   <Setter Property="Fill" Value="Green"/>
</DataTrigger>

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>
帅哥哥的热头脑 2024-10-19 06:39:37

工作原理如下:

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

编辑:您需要将触发器放置在 Window.Style 中。

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.

玉环 2024-10-19 06:39:37

窗口最大化时如何增加边框厚度的示例。否则,由于 WindowChrome 的怪异,边框将会消失。

此示例还删除了标准窗口标题,因此您必须添加自己的最小化/最大化/关闭按钮。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文