向RichTextBlock中的超链接添加交互行为

时间:2017-03-13 12:03:30

标签: c# xaml uwp win-universal-app

我有RichTextBlock Hyperlink

我想在单击超链接时从我的ViewModel执行Command,因此我使用了来自 Microsoft.Xaml.Behaviors 的交互Behavior

<RichTextBlock>
    <Paragraph>
        <Hyperlink>
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction
                        Command="{Binding ShowDocumentCommand}" />
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
            <Hyperlink.Inlines>
                <Run Text="Some link" />
            </Hyperlink.Inlines>
        </Hyperlink>
    </Paragraph>
</RichTextBlock>

但它不起作用。它运行,但没有任何反应。单击Hyperlink时不执行该命令。

为什么呢? 我该怎么做才能让它执行命令?

1 个答案:

答案 0 :(得分:2)

看起来行为只能附加到派生自FrameworkElement的类。但是Hyperlink并没有继承它。

您只需使用UWP社区工具包中的HyperlinkExtensions,该工具包已包含所需的附加属性CommandCommandParameter。或者您可以从github复制粘贴代码。

所以你的代码看起来像那样:

<RichTextBlock>
    <Paragraph>
        <Hyperlink xaml:HyperlinkExtensions.Command="{Binding ShowDocumentCommand}">
            <Hyperlink.Inlines>
                <Run Text="Some link" />
            </Hyperlink.Inlines>
        </Hyperlink>
    </Paragraph>
</RichTextBlock>
相关问题