syn3rgy
syn3rgy

Reputation: 115

Get the text from clicked Hyperlink

I created some Hyperlinks using the code below:

 public Class_List()
{
    InitializeComponent();

    StackPanel myStackPanel = new StackPanel();
    TextBlock txt = new TextBlock();
    txt.Foreground = Brushes.Black;
    txt.FontFamily = new FontFamily("03SmartFontUI");
    txt.FontSize = 25;
    txt.Margin = new Thickness(0, 5, 0, 5);
    Run run = new Run(className);
    Hyperlink link = new Hyperlink(run);                                    
    link.Click += Link_Click;                                               
    txt.Inlines.Add(link);
}

Now, I want to get the text of the hyperlink and store it on string s:

private void Link_Click(object sender, RoutedEventArgs e)
{
    string s = (sender as Hyperlink).Inlines.ToString();

    Class_Page class_page = new Class_Page();
    NavigationService.Navigate(class_page);
}

However instead of the hyperlink text, I got

System.Windows.Documents.InlineCollection

Upvotes: 1

Views: 373

Answers (1)

Meloviz
Meloviz

Reputation: 581

You're getting that type because you are actually accessing the entire collection of Inlines rather than the Inline you're looking for. The fastest way to access the Run's text you're using as the first Inline in the Hyperlink's InlineCollection is to do this:

((sender as Hyperlink).Inlines.FirstInline as Run).Text;

Upvotes: 1

Related Questions