按钮样式取决于Window.IsActive

时间:2013-10-09 10:29:39

标签: wpf xaml .net-3.5

我需要有一个按钮根据Window.IsActive属性更改其样式 - 当窗口失去焦点时,前景色应该会改变。

以下代码不会导致任何更改:

<Style TargetType="Button"
       x:Key="SYS_Window_Button">
    <Setter Property="Foreground"
            Value="{StaticResource SYS_Window_Button_Text}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="MyContentPresenter"
                                      Content="{TemplateBinding Content}"
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Margin="0" />
                </Border>

                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=IsActive}"
                                 Value="False">
                        <Setter Property="Foreground"
                                Value="Red" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

有什么想法吗?

0 个答案:

没有答案