Search code examples
wpfhyperlinktextblock

not to set Hyperlink when NavigateUri is null


I am using Hyperlink in TextBlock. The problem I am facing is when NavigateUri is null, I don't want to set Hyperlink or use default style, so that there is no difference between TextBlock and Hyperlink. How to do this?

The code that I am using is this:

<TextBlock TextWrapping="Wrap">
  <Hyperlink NavigateUri="{Binding Path=Href}" RequestNavigate="Hyperlink_RequestNavigate">
    <Run Text="{Binding Path=Body}"/>
  </Hyperlink>
</TextBlock>  

Sometimes Href is null. That time I don't have to set NavigateUri.


Solution

  • The solution I used is using DataTrigger to check Href value, if is equals to Null, set the related properties to imitate TextBlock's style

    <Style TargetType="{x:Type Hyperlink}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Href}" Value="{x:Null}">
                        <Setter Property="Foreground" Value="Black" />
                        <Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
                        <Setter Property="Cursor" Value="Arrow" />
                    </DataTrigger>
                </Style.Triggers>
    
            </Style>
    

    Null value:

    Null value

    !Null Value:

    !Null Value