通过结合它可点击链接动态创建的文本 [英] Dynamically created text with clickable links in it via binding

查看:89
本文介绍了通过结合它可点击链接动态创建的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建它由一个自己的类的多个项目的列表视图。一个属性是它可以包含在它的一个或多个链接文本。通常,我使用一个TextBlock以获得该内容显示在文本绑定。

I do want to create a listview which consists of many items of an own class. One of the properties is a text which can contain one or more links in it. Normally I use a textblock with a binding on Text to get this content displayed.

现在我确实想被解析链接的文本,然后动态地使这些链接点击。我发现相当长的一段code像<一个href=\"http://stackoverflow.com/questions/2092890/add-hyperlink-to-textblock-wpf\">http://stackoverflow.com/questions/2092890/add-hyperlink-to-textblock-wpf关于如何创建带超级链接的文本块,所以我会被罚款 - 但WPF绑定可在文本属性,因此这并不能帮助我到底

Now I do want those text being parsed for links and then dynamically make those links clickable. I found quite some code like http://stackoverflow.com/questions/2092890/add-hyperlink-to-textblock-wpf on how to create a textblock with hyperlinks so I would be fine - but the WPF binding is available on the Text property so this doesn't help me in the end.

那么,有没有在列表视图的项目(的ObservableCollection或类似)的列表结合有文本中点击链接的方式?

So is there a way for binding for a list of items (ObservableCollection or similar) in a listview to have clickable links within text?

Thx提前

斯文

推荐答案

我有一个简单的解决方案。

I have a simple solution.

使用DataTemplate中,你可以指定一个类的模板,说LinkItem其中包含文本和超链接。

Using the DataTemplate, you could specify an template for a class, say LinkItem which contains your text, and a hyper-link.

public class LinkItem
{
    public string Text { get; set; }
    public string Hyperlink { get; set; }

    public LinkItem(string text, string hyperlink)
    {
        Text = text;
        Hyperlink = hyperlink;
    }
}

// XAML Data template
<DataTemplate DataType="{x:Type HyperlinkDemo:LinkItem}">
    <TextBlock>
        <TextBlock Text="{Binding Text}" Margin="1" />
        <Hyperlink>
            <TextBlock Text="{Binding Hyperlink}" Margin="1" />
        </Hyperlink>
    </TextBlock>
</DataTemplate>

// List box definition
<ListBox ItemsSource="{Binding LinkItems}" />

尼斯和简单。只是一堆LinkItem添加到您的收藏LinkItems,你会得到的文本和超链接在你的列表框中的一些不错的组合。

Nice and simple. Just add a bunch of LinkItem to your LinkItems collection and you will get some nice mix of text and hyperlink in your list box.

您也可以扔在一个命令,在LinkItem类让事情变得更有趣和命令绑定到超链接。

You could also throw in a command in the LinkItem class to make things a little more interesting and bind the command to the hyperlink.

<Hyperlink Command="{Binding HyperlinkCommand}"> ....

这篇关于通过结合它可点击链接动态创建的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆