syn3rgy syn3rgy - 6 months ago 25
C# Question

Get the text from clicked Hyperlink

I created some Hyperlinks using the code below:

public Class_List()

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;

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();

However instead of the hyperlink text, I got



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;