c# - 标签样式未在窗口状态更改时更新

标签 c# wpf xaml

我正在尝试根据 WindowState 属性更新 Label 的内容.. 我有以下内容:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="StyleMinMax" TargetType="{x:Type Label}">
            <Setter Property="TextElement.FontSize" Value="22" />
            <Setter Property="TextElement.FontWeight" Value="SemiBold" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Foreground" Value="LightSlateGray" />
            <Style.Triggers>
                <Trigger Property="Window.WindowState" Value="Normal">
                    <Setter Property="Content" Value="1-Normal" />
                </Trigger>
                <Trigger Property="Window.WindowState" Value="Maximized">
                    <Setter Property="Content" Value="2-Maximized" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <Label Style="{StaticResource StyleMinMax}"></Label>
    </Grid>
</Window>

应用启动时标签内容会有1-Normal,但是当我最大化我的应用时,内容没有变化.. 有什么想法吗?

提前致谢

最佳答案

将触发器更新为 DataTriggers

     <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="Normal">
             <Setter Property="Content" Value="1-Normal" />
      </DataTrigger>
     <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"  Value="Maximized">
             <Setter Property="Content" Value="2-Maximized" />
      </DataTrigger>

关于c# - 标签样式未在窗口状态更改时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18740235/

相关文章:

c# - 如何获取正在执行的.csx脚本的路径?

c# - ImageSourceConverter 抛出 NullReferenceException ... 为什么?

wpf - 带导航的ICommand

c# - 使用 Windows 8 Metro 的 WebView 中的 JavaScript 错误

c# - 如何通过非主键创建一对一关系(EF First Code)

c# - WPF 单选按钮组与自定义用户控件发生冲突

wpf - 使用MVVM在View中更改ContentControl的ControlTemplate

c# - 显示模态对话框并获取结果

c# - WPF Datagrid - 强制单行行

xaml - 使用收件人、抄送和密件抄送时,如何为 Outlook 中已知的 UWP 创建标记化控件