问题
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
.
回答1:
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:
来源:https://stackoverflow.com/questions/37252768/not-to-set-hyperlink-when-navigateuri-is-null