tags:

views:

131

answers:

2

The content of the textblock is imported from an webservice, somehow, there is URL, is it possible to make it like a link?

Thanks.

A: 

You could do something like the following; however this makes use of a Label and not a textblock.

In your XAML you do the following:

<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>

and in your code behind you add the following dependency property and set the datacontext to the page itself

public static readonly DependencyProperty URLProperty =
            DependencyProperty.Register("URI", typeof(Uri), typeof(MainPage), null);

        public Uri URI { get
            {
                return (Uri)GetValue(URLProperty);
            }
            set
            { SetValue(URLProperty, value); }
        }

This code sets the dependency property for the binding to the URL;

public MainPage()
        {
            InitializeComponent();
            URI = new Uri("/Home", UriKind.Relative);
            DataContext = this;
        }

This code creates a new URI and binds it to the variable. It also sets the data context to the page itself.

Johannes
A: 

Sounds like you want a LinkLabel control. I've used that control with some modifications in my Silverlight Twitter Badge to mix the text and links that show up in tweets.

If you just have a TextBlock with a link only and want that clickable then you just set the cursor to be a hand and add an event handler for the MouseLeftButtonDown event that would navigate to the value of the TextBox.

Xaml:

<TextBlock Text="http://www.microsoft.com" Cursor="Hand" TextDecorations="Underline" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />

Code:

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var txt = ((TextBlock)sender).Text;
    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(txt, UriKind.Absolute));
}
Bryant