在Wpf Rich Textbox

时间:2017-11-07 07:35:03

标签: c# wpf hyperlink

我有一个非常简单的屏幕,显示一些内容和超链接。由于超链接分布在多行,我不能使用WPF按钮,因为它会将整个链接移动到客户端无法接受的新行。

这是一个Caliburn微型MVVM应用程序,我在视图模型中处理在我的方法中打开链接的过程。我不想使用代码隐藏,因此需要绑定超链接的RequestNavigate事件。

富文本框的代码是

 <RichTextBox FontSize="13"  BorderThickness="0" IsDocumentEnabled="True" IsReadOnly="True" Block.TextAlignment="Center"  Width="270">
        <FlowDocument>
            <Paragraph>
                Download Failed. Please
                <Bold>check your connection and try again or</Bold>
                <Hyperlink >contact your provider</Hyperlink>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>

输出

enter image description here

1 个答案:

答案 0 :(得分:0)

Hyperlink实现了ICommandSource接口,因此您可以使用它的Command属性绑定到ViewModel上的Command,就像使用Button一样。

<Hyperlink Command="{Binding MyNavigationRequestedCommand}">contact your provider</Hyperlink>

我猜你已经熟悉了实现,因为你提到你会使用Button,但这里有一个link to an explanation。如果您需要专门处理RequestNavigate事件,则可以使用EventTrigger中的InvokeCommandAction + System.Widnows.Interactivity,如here所述。