c# - 项目源文本到超链接

我有一个我想制作超链接的链接列表,锚标记不起作用。还有另一种方法可以将字符串转换为链接吗?我正在使用<ItemsControl>绑定到LinkList.Link


这是我尝试过的:


for (int i = 0; i < this.LinkList.Link.Count; i++)

{

    var link = this.LinkList.Link[i];

    this.LinkList.Link[i] = "<a href=\"" + link + "\">" + link + "</a>";

           //String.Format("<a href=\"{0}\">{0}</a>", link );

}

两者(在评论中)都不起作用。


茅侃侃
浏览 219回答 2
2回答

红糖糍粑

在 ItemsControl 的 ItemTemplate 中使用超链接如何,如下所示:&nbsp; &nbsp; <ItemsControl ItemsSource="{Binding LinkList}">&nbsp; &nbsp; &nbsp; &nbsp; <ItemsControl.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Hyperlink NavigateUri="{Binding Link}" RequestNavigate="Hyperlink_RequestNavigate">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{Binding Link}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Hyperlink>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </ItemsControl.ItemTemplate>&nbsp; &nbsp; </ItemsControl>如您所见,超链接上还有一个事件“RequestNavigate”,它在代码后面有一个处理程序,如下所示:&nbsp; &nbsp;private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);&nbsp; &nbsp; &nbsp; &nbsp; e.Handled = true;&nbsp; &nbsp;}或者,当然,您可以绑定超链接的命令以使用 MVVM 模式执行导航。希望有帮助。

慕后森

<ListBox ItemsSource="{Binding LinkList, Mode=OneWay}">&nbsp;<ListBox.ItemTemplate>&nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<Hyperlink NavigateUri="{Binding Link}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Link Text&nbsp; &nbsp; &nbsp; &nbsp; </Hyperlink>&nbsp; &nbsp; </DataTemplate>&nbsp;</ListBox.ItemTemplate></ListBox>您可能需要将 XAML 模板作为绑定到 url 的超链接。
打开App,查看更多内容
随时随地看视频慕课网APP