首页 > 解决方案 > C# WPF MVVM 将命令绑定到自定义文本块控件中的超链接

问题描述

我有一个带有字符串属性的项目集合。该字符串属性包含在不同位置包含 6 位数字的文本,如下所示:

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

998552

我想将这些 6 位数字转换为超链接,单击这些超链接将在 ViewModel 上运行命令,并将自身作为参数传递。例如,如果我单击 401177,我想使用字符串参数“401177”在 VM 上运行 HyperlinkCommand。我仍然想保留原始文本的格式。

我认为最好的方法是使用基于 TextBlock 的自定义控件。下面是我的视图的粗略结构,UserControl 绑定到 ViewModel,我使用 ContentControl 绑定到具有属性“detail”的项目集合,并且使用绑定到“detail”的自定义文本块进行模板化我的物品的财产。

<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>

我使用了这个问题的代码并稍微编辑它以生成以下自定义控件:

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

这正确显示了链接,但是我不知道如何绑定到我的 ViewModel 上的命令。我之前使用按钮测试了命令和参数,并且绑定是

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

所以我希望我可以将此 XAML 转换为 C# 并将其附加到hyperLink.Command =我的自定义控件中。我不知道如何访问将放置 CustomTextBlock 的 UserControl 的 DataContext。

我并不幻想我正在做的事情是最好或正确的做事方式,所以我欢迎任何建议

标签: c#wpfmvvm

解决方案


这是一个有趣的挑战,我已经用新代码解决了这个问题 - 以稍微不同的方式解决问题:

代码可以在这里找到: https ://github.com/deanchalk/InlineNumberLinkControl


推荐阅读