0

I have a WPF app i'm working on.

There is a label on my window I use as a status window that I manage with a singleton.

How do i insert a link into that that label?

All the answers I see on this have to do with changing the XAML or the nature of the label. These solutions don't work, because literally anything can appear in this status window.

I want the link to raise a custom event when clicked.

Does anyone know how to do this?

Example of how the text uploaded to the label should operate. I need to raise an event when the hypertext formatted word is clicked:

enter image description here

Jamie Marshall
  • 1,885
  • 3
  • 27
  • 50
  • 1
    [Hyperlink](https://stackoverflow.com/questions/140996/how-can-i-set-the-text-of-a-wpf-hyperlink-via-data-binding). – Lei Yang Jul 11 '17 at 02:22
  • @Lei Yang, doesn't work. I don't think any XAML solution will. I need to insert a single link into text, and post that text to my label. I'm fairly positive whatever the solution is it will be entirely C#. – Jamie Marshall Jul 11 '17 at 03:21
  • can you upload a screen capture about what your expected result is? – Lei Yang Jul 11 '17 at 03:24
  • I added a picture to to the question. Ignore the line going vertically through the image, its just a guide from the designer. – Jamie Marshall Jul 11 '17 at 03:39
  • that is hyperlink – Lei Yang Jul 11 '17 at 04:41
  • 1
    _"literally anything can appear in this status window"_ -- really? Like, an actual **cow** could appear there? What does that mean, and why does that preclude changes to the XAML? Your question is too vague and unclear. Please provide a good [mcve] that shows how your scenario works now, and explain _precisely_ what it is you want the code to do. Let other people worry about what's possible and what's not; you are getting ahead of yourself, rejecting solutions before you actually know what a working solution would look like. – Peter Duniho Jul 11 '17 at 05:09

1 Answers1

1

"Inserting a link into text" is the same thing as adding a Hyperlink to the Inlines collection of a TextBlock:

TextBlock tb = new TextBlock();
var hp = new Hyperlink(new Run("error"));
hp.Click += (s, e) => { /* do something */ };
tb.Inlines.Add(new Run("There was as an "));
tb.Inlines.Add(hp);
tb.Inlines.Add(new Run(" on run"));

This is the one and only way to do this in WPF and it is very simple.

mm8
  • 163,881
  • 10
  • 57
  • 88