在Windows 8 Metro应用程序中将超链接绑定到richtextblock [英] Binding Hyperlink to richtextblock in windows 8 Metro app

查看:66
本文介绍了在Windows 8 Metro应用程序中将超链接绑定到richtextblock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Windows 8 Metro应用,用于娱乐/学习等.
我创建了一个包含描述,图像等文本项的列表视图.在描述中,经常有一些我想使其可单击的超链接.

I am building a windows 8 metro app for fun/learning etc.
I have created a listview of text items that have descriptions, images etc. Inside the description, there are often hyperlinks that I would like to make clickable.

但是,当绑定到文本块时,xaml超链接代码将显示为文本.搜索arround,看来我需要对超链接使用一个richtextblock.我似乎无法弄清楚如何将超链接绑定到它.我从wpf中发现了许多示例,这些示例显示了如何使用flowdocument扩展richtextblock.当前框架的使用者预览版中不存在Flowdocument.

However, when binding to a textblock, xaml hyperlink code is displayed as text. Searching arround, it looks like I need to use a richtextblock for hyperlinks. I can't seem to figure out how to bind a hyperlink to it. I have found many examples from wpf showing how to extend the richtextblock using flowdocument. Flowdocument doesn't exist in the current consumer preview version of the framework.

我正在伸出援手,看看是否有人解决了这个问题,或者对前进的道路有何建议.

I am reaching out to see if anyone has solved this issue or has any suggestions on what path to head down.

我目前拥有的代码

现在我只是将状态对象中的文本"字段绑定到文本"上的文本块绑定

right now I am just binding the "text" field from my Statuses Object to a textblock binding on "text"

我在文本字段中具有要使其可点击的URL.

I have URL's in the text field which I want to be able to make clickable.

作为测试,我将第一个对象的文本字段替换为超链接标记

As a test I was replacing the text field of the first object with hyperlink markup

例如 feed_results [0] .text ="<hyperlink .....

ex. feed_results[0].text = "<hyperlink .....

然后尝试绑定到texblock和richtextblock

then trying to bind to texblock and richtextblock

Xaml

<ListView x:Name="ItemListView" ItemsSource="{Binding}"  Background="Black" Width="372" VerticalAlignment="Top" Margin="50,0,0,0">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel  Orientation="Vertical"  MinHeight="100">
                            <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding user.profile_image_url}" Margin="0,0,15,0" VerticalAlignment="Top" />
                            <StackPanel Orientation="Vertical">
                                    <TextBlock HorizontalAlignment="Left" Foreground="Gray" Text="{Binding user.name}" FontWeight="Bold" TextWrapping="Wrap"  MaxWidth="200" />
                                    <TextBlock HorizontalAlignment="Left" Foreground="Gray"   Text="{Binding text}" TextWrapping="Wrap"  MaxWidth="200" />                      
                                </StackPanel>
                            </StackPanel>
                            <StackPanel  Margin="0,15,0,0" HorizontalAlignment="Right">
                                <TextBlock Text="{Binding created_at,  Converter={StaticResource StringConverter},ConverterParameter=Released: \{0:d\}}"   HorizontalAlignment="Center" Foreground="Gray" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>

后端代码

FeedResult<Statuses> r2 = await feed.StatusesAsync(1, 50);

            if (!r2.HasError)
            {
                feed_results = r2.Result;


                Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.High, new Windows.UI.Core.InvokedHandler((o, a) =>
                {

                    ItemListView1.ItemsSource = feed_results;
                }), this, null);
            }

推荐答案

Microsoft从Metro XAML中删除了对内联超链接的支持.您仍然可以使用 HyperlinkBut​​ton 用于非内联超链接,或者如果您的内联超链接较短(1-2个字),则可以将HyperlinkBut​​ton放在RichTextBlock中的InlineUIContainer中.以后的解决方案将需要一些代码,仅使用绑定将无法实现.

Microsoft removed support for inline hyperlinks from Metro XAML. You can still use HyperlinkButton for non inline hyperlinks, or if your inline hyperlinks short (1-2 words) then you could place HyperlinkButton inside of InlineUIContainer in RichTextBlock. Later solution would require some code, just using binding won't do it.

这篇关于在Windows 8 Metro应用程序中将超链接绑定到richtextblock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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