Silverlight 如何使textblock中的URL可单击?
textblock的内容是从web服务导入的,但不知怎的,它有一个URL 有可能把它变成一个链接吗Silverlight 如何使textblock中的URL可单击?,silverlight,Silverlight,textblock的内容是从web服务导入的,但不知怎的,它有一个URL 有可能把它变成一个链接吗 谢谢。你可以做如下事情:;但是,这将使用标签而不是文本块 在XAML中,您可以执行以下操作: <dataInput:Label Grid.Row="2"> <ContentPresenter> <StackPanel Orientation="Horizontal">
谢谢。你可以做如下事情:;但是,这将使用标签而不是文本块 在XAML中,您可以执行以下操作:
<dataInput:Label Grid.Row="2">
<ContentPresenter>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hello world"/>
<HyperlinkButton x:Name="Test" NavigateUri="{Binding Path=URI}" Content="This is a url"/>
</StackPanel>
</ContentPresenter>
</dataInput:Label>
此代码设置绑定到URL的依赖项属性
public MainPage()
{
InitializeComponent();
URI = new Uri("/Home", UriKind.Relative);
DataContext = this;
}
此代码创建一个新URI并将其绑定到变量。它还将数据上下文设置为页面本身。听起来像是您想要的。我已经使用该控件,并在我的中进行了一些修改,以混合tweet中显示的文本和链接
如果您只有一个带有链接的TextBlock,并且希望它是可单击的,那么您只需将光标设置为手,并为MouseLeftButtonDown事件添加一个事件处理程序,该事件将导航到TextBox的值
Xaml:
你看过这个问题吗?它似乎达到了你所需要的。
public MainPage()
{
InitializeComponent();
URI = new Uri("/Home", UriKind.Relative);
DataContext = this;
}
<TextBlock Text="http://www.microsoft.com" Cursor="Hand" TextDecorations="Underline" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var txt = ((TextBlock)sender).Text;
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(txt, UriKind.Absolute));
}