2017-08-30 81 views
1

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

我都卡在这里:

  <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> 
+1

将Foreground属性绑定到IsEnabled属性并使用转换器来更改颜色。 –

+0

@VishalPrajapati谢谢。我认为这会起作用 – ZigZig

回答

2

当超链接被禁用,它会改变颜色在默认情况下(如果默认前景不被覆盖)为灰色,这样你就可以禁用的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> 

如果超级链接应该有非默认的主动/禁用的颜色,你可以写为超链接的风格,设有触发:

<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>