Reputation: 13992
I have a RichTextBlock
with a Hyperlink
.
I would like to execute a Command
from my ViewModel when the Hyperlink is clicked, so I used a Interaction Behavior
from Microsoft.Xaml.Behaviors:
<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>
But it doesn't work. It runs, but nothing happens. The command isn't executed when the Hyperlink
is clicked.
Why? What should I do to make it execute the command?
Upvotes: 2
Views: 265
Reputation: 910
It looks like Behaviors can be only attached to the classes derived from FrameworkElement. But Hyperlink does not inherit from it.
You can simply use HyperlinkExtensions from UWP Community Toolkit package, which already has the required attached properties Command
and CommandParameter
. Or you can just copy-paste their code from github.
So your code will look like that:
<RichTextBlock>
<Paragraph>
<Hyperlink xaml:HyperlinkExtensions.Command="{Binding ShowDocumentCommand}">
<Hyperlink.Inlines>
<Run Text="Some link" />
</Hyperlink.Inlines>
</Hyperlink>
</Paragraph>
</RichTextBlock>
Upvotes: 2