0

I have a collection of items with a string property. That string property contains text which includes 6 digit numbers in various places like so:

this string 123456 is an example of a set of links 884555 to the following numbers
401177
155879

998552

I want to turn those 6 digit numbers into hyperlinks that when clicked will run a command on the ViewModel passing themselves as parameters. For example if I click 401177 I want to run HyperlinkCommand on the VM with the string parameter "401177". I still want to keep the formatting of the original text.

I figured the best way to do it would be with a custom control based on TextBlock. Below is the rough structure of my view, the UserControl is bound to the ViewModel, I use a ContentControl to bind to a collection of items with the property "detail", and that is templated with the custom text block bound to the "detail" property of my items.

<UserControl.DataContext>
    <VM:HdViewModel/>
</UserControl.DataContext>
<UserControl.Resources>
    <DataTemplate x:Key="DetailTemplate">
        <StackPanel Margin="30,15">
            <helpers:CustomTextBlock FormattedText="{Binding detail}"/>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
<Grid>                   
    <ContentControl Content="{Binding ItemListing}" ContentTemplate="{StaticResource DetailTemplate}" />
</Grid>

I used the code from this question and edited it slightly to generate the following custom control:

public class CustomTextBlock : TextBlock
{
    static Regex _regex = new Regex(@"[0-9]{6}", RegexOptions.Compiled);

    public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached("FormattedText", typeof(string), typeof(CustomTextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure, FormattedTextPropertyChanged));
    public static void SetFormattedText(DependencyObject textBlock, string value)
    {
        textBlock.SetValue(FormattedTextProperty, value);
    }

    public static string GetFormattedText(DependencyObject textBlock)
    { return (string)textBlock.GetValue(FormattedTextProperty); }

    static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!(d is TextBlock textBlock)) return;

        var formattedText = (string)e.NewValue ?? string.Empty;
        string fullText =
            $"<Span xml:space=\"preserve\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">{formattedText}</Span>";

        textBlock.Inlines.Clear();
        using (var xmlReader1 = XmlReader.Create(new StringReader(fullText)))
        {
            try
            {
                var result = (Span)XamlReader.Load(xmlReader1);
                RecognizeHyperlinks(result);
                textBlock.Inlines.Add(result);
            }
            catch
            {
                formattedText = System.Security.SecurityElement.Escape(formattedText);
                fullText =
                    $"<Span xml:space=\"preserve\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">{formattedText}</Span>";

                using (var xmlReader2 = XmlReader.Create(new StringReader(fullText)))
                {
                    try
                    {
                        dynamic result = (Span)XamlReader.Load(xmlReader2);
                        textBlock.Inlines.Add(result);
                    }
                    catch
                    {
                        //ignored
                    }
                }
            }
        }
    }

    static void RecognizeHyperlinks(Inline originalInline)
    {
        if (!(originalInline is Span span)) return;

        var replacements = new Dictionary<Inline, List<Inline>>();
        var startInlines = new List<Inline>(span.Inlines);
        foreach (Inline i in startInlines)
        {
            switch (i)
            {
                case Hyperlink _:
                    continue;
                case Run run:
                    {
                        if (!_regex.IsMatch(run.Text)) continue;
                        var newLines = GetHyperlinks(run);
                        replacements.Add(run, newLines);
                        break;
                    }
                default:
                    RecognizeHyperlinks(i);
                    break;
            }
        }

        if (!replacements.Any()) return;

        var currentInlines = new List<Inline>(span.Inlines);
        span.Inlines.Clear();
        foreach (Inline i in currentInlines)
        {
            if (replacements.ContainsKey(i)) span.Inlines.AddRange(replacements[i]);
            else span.Inlines.Add(i);
        }
    }

    static List<Inline> GetHyperlinks(Run run)
    {
        var result = new List<Inline>();
        var currentText = run.Text;
        do
        {
            if (!_regex.IsMatch(currentText))
            {
                if (!string.IsNullOrEmpty(currentText)) result.Add(new Run(currentText));
                break;
            }
            var match = _regex.Match(currentText);

            if (match.Index > 0)
            {
                result.Add(new Run(currentText.Substring(0, match.Index)));
            }

            var hyperLink = new Hyperlink();
            hyperLink.Command = ;
            hyperLink.CommandParameter = match.Value;
            hyperLink.Inlines.Add(match.Value);
            result.Add(hyperLink);

            currentText = currentText.Substring(match.Index + match.Length);
        } while (true);

        return result;
    }
}

This is showing the links properly, however I dont know how to bind to the command on my ViewModel. I tested the command and the parameter using a button previously, and the binding was

Command="{Binding DataContext.HyperlinkCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"

So what I am hoping is that I can convert this XAML into C# and attach it to hyperLink.Command = in my custom control. I can't figure out how to access the DataContext of the UserControl that the CustomTextBlock will be placed in.

I am not under any illusion that what I am doing is the best or right way of doing things so I welcome any suggestions

Ashley
  • 3
  • 2
  • This seems a really complicated way to create a set of links from data that can fire commands. Personally I would have used an ItemsControl with a ItemTemplate that shows a button styled as a hyperlink. It would be very simple to do what you want that way – Dean Chalk Oct 29 '21 at 11:11
  • Would I have to break the text up into "non-links" and "links", then put it back together using textblocks for the string and buttons for the hyperlinks? I did try doing that and it worked, but it seemed to stack the items rather than just flow like a textblock. so the formatting wasn't preserved – Ashley Oct 29 '21 at 11:32
  • So, are you saying that there is a text string that includes numbers (which need to be links), and standard text (that isnt a link) ? – Dean Chalk Oct 29 '21 at 11:35
  • That is correct, so the example right at the very top would result in the textblock showing the same text in the same format, but with the five 6-digit numbers turned into hyperlinks (123456, 884555, 401177, 155879 and 998552) running the same command on the ViewModel but passing their own value as a parameter. Apologies if the question wasn't clear – Ashley Oct 29 '21 at 11:41
  • That is a really interesting problem, that I believe is best solved as a custom control. Let me have a play around, and Ill post some code here that works when Ive found the answer – Dean Chalk Oct 29 '21 at 11:43
  • Ive coded a solution - see my answer - hope it helps – Dean Chalk Oct 29 '21 at 12:40

1 Answers1

0

This is an interesting challenge, which I have solved with new code - coming at the problem in a slightly different way:

The code can be found here: https://github.com/deanchalk/InlineNumberLinkControl

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • Many thanks for this Dean, its excellent. I am struggling to implement it in my own project though, I presume its because I target framework 4.8 instead of 5.0 so I don't have C# 9.0 support. In the OnTextPropertyChanged, I have converted `if (d is not TextNumberLinkControl lControl || lControl._textBlock == null) return; lControl.ResetText();` to `var lControl = d as TextNumberLinkControl; if (!(d is TextNumberLinkControl) || lControl._textBlock == null) return; lControl.ResetText();` however _textBlock is always null. – Ashley Oct 29 '21 at 13:36
  • Your code is correct. Are you sure you have added the 'Generic.xaml' file ? It MUST be in a folder called 'Themes' – Dean Chalk Oct 29 '21 at 14:02
  • Ive added a .NET Framework version of the same code to the repository - if it helps – Dean Chalk Oct 29 '21 at 14:12
  • Also, as well as making sure you have Generic.xaml in your Themes directory, you need to make sure your AssemblyInfo.cs files contains these theme parameters [assembly: ThemeInfo( ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly )] – Dean Chalk Oct 29 '21 at 14:15
  • That is exactly what I was missing, thank you. Working exactly as I hoped it would! – Ashley Oct 29 '21 at 14:37
  • I just noticed that it only runs up to the end of the last hyperlink and truncates any text after that. In order to fix that I added the following 2 lines in the ResetText method immediately after the foreach: `var finalText = Text.Substring(cursor, Text.Length - cursor); _textBlock.Inlines.Add(new Run(finalText));` – Ashley Oct 29 '21 at 14:59
  • Yeah, I guess it wouldnt be hard to finish the code – Dean Chalk Oct 29 '21 at 15:39