如何在更改 TextBlock 的 IsEnabled 属性 时更改超链接的颜色?

How to change the Hyperlink's colour when the IsEnabled property of a TextBlock is changed?

我是 WPF 新手。当 TextBlockIsEnabled 属性 变为 false 时,我试图将超链接的前景色更改为其他颜色(比如灰色)。我要加一个Style来达到我的要求吗?

我卡在这里了:

            <TextBlock Margin="0,150,0,0"
                       TextAlignment="Center" 
                       Background="White" 
                       IsEnabled="{Binding ShowProgressRing}">

                <Hyperlink x:Name="HyperLink"
                           Foreground="Blue"
                           TextDecorations="UnderLine"
                           FontSize="12"
                           FontWeight="SemiBold" 
                           Command="{Binding Path=Command}" >
                    <Run Text="{Binding Path=HyperLinkText}"/>
                </Hyperlink>
            </TextBlock>

当超链接被禁用时,默认情况下它会将颜色更改为灰色(如果未覆盖默认前景),因此您可以禁用 TextBlock 并完成

<TextBlock Margin="0,150,0,0"
           TextAlignment="Center" 
           Background="White"
           IsEnabled="{Binding ShowProgressRing}">

    <Hyperlink x:Name="HyperLink"                   
               TextDecorations="UnderLine"
               FontSize="12"
               FontWeight="SemiBold" 
               Command="{Binding Path=Command}" >
        <Run Text="{Binding Path=HyperLinkText}"/>
    </Hyperlink>
</TextBlock>

如果超链接应具有非默认 active/disabled 颜色,您可以使用触发器为超链接编写样式:

<Hyperlink x:Name="HyperLink" 
            TextDecorations="UnderLine"
            FontSize="12"
            FontWeight="SemiBold" 
            Command="{Binding Path=Command}" >

    <Run Text="{Binding Path=HyperLinkText}"/>

    <Hyperlink.Style>
        <Style TargetType="Hyperlink">
            <Setter Property="Foreground" Value="Blue"/>
            <Style.Triggers>
                <!--binding to the same view model property which sets IsEnabled-->
                <DataTrigger Binding="{Binding ShowProgressRing}" Value="False">
                    <Setter Property="Foreground" Value="Gray"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Hyperlink.Style>

</Hyperlink>