标签样式未更新windowstate已更改

时间:2013-09-11 11:43:52

标签: 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,但是当我最大化我的应用程序时,内容不会更改.. 任何想法?

提前致谢

1 个答案:

答案 0 :(得分:6)

将触发器更新为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>