从单击的超链接中获取文本

时间:2016-09-20 11:20:17

标签: c# .net wpf hyperlink

我使用以下代码创建了一些超链接:

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

现在,我想获取超链接的文本并将其存储在字符串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);
}

然而,我得到了

而不是超链接文本
  

System.Windows.Documents.InlineCollection

1 个答案:

答案 0 :(得分:1)

您正在获取该类型,因为您实际上正在访问Inline的整个集合,而不是您正在寻找的Inline。访问Run Inline Hyperlink InlineCollection ((sender as Hyperlink).Inlines.FirstInline as Run).Text; Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8 INFO: Java 1.7.0_80 (64-bit) INFO: Windows 7 6.1 amd64 INFO: Download javascriptcustom-2.1.4.jar INFO: No analyzers installed INFO: Index files INFO: 0 files indexed INFO: ------------- SonarLint Report ------------- No files analyzed ------------------------------------------- INFO: SonarLint HTML Report generated: D:\GScanOjetRules\sonarlint-cli1.0\sonarlint-cli-1.0\bin\.sonarlint\sonarlint-report.html INFO: ---------------------------------------------------------------------- -- INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 0.991s INFO: Final Memory: 2M/118M INFO: ------------------------------------------------------------------------ ORDER BY TOP的{​​{1}}文本的最快方法是:

FETCH