2

我需要一个按钮根据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>

有任何想法吗?

4

0 回答 0