1

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

syn3rgy
  • 115
  • 1
  • 1
  • 11

1 Answers1

1

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;
Meloviz
  • 571
  • 6
  • 16