我有一個帶有字串屬性的專案集合。該字串屬性包含文本,其中在不同位置包含 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>
我使用了this question中的代碼并對其進行了輕微編輯以生成以下自定義控制元件:
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。
我并不認為我正在做的事情是最好或正確的做事方式,所以我歡迎任何建議
uj5u.com熱心網友回復:
這是一個有趣的挑戰,我用新代碼解決了這個問題——以稍微不同的方式解決這個問題:
代碼可以在這里找到:https : //github.com/deanchalk/InlineNumberLinkControl
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341587.html
上一篇:確定一個值在c#中的范圍
