not to set Hyperlink when NavigateUri is null

末鹿安然 提交于 2020-12-16 05:36:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!