110,539
社区成员
发帖
与我相关
我的任务
分享
public static class InlineTextBehaviour
{
public static string GetInlineText(DependencyObject obj)
{
return (string)obj.GetValue(InlineTextProperty);
}
public static void SetInlineText(DependencyObject obj, string value)
{
obj.SetValue(InlineTextProperty, value);
}
public static readonly DependencyProperty InlineTextProperty = DependencyProperty.RegisterAttached(
"InlineText",
typeof(string),
typeof(InlineTextBehaviour),
new PropertyMetadata(null, InlineTextChanged)
);
static void InlineTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var textBlock = sender as TextBlock;
if (textBlock != null)
{
textBlock.Inlines.Clear();
textBlock.Inlines.Add(FormatText(e.NewValue as string ?? ""));
}
}
static Dictionary<string, Func<string, Run>> keywords = new Dictionary<string, Func<string, Run>>()
{
{"张老三", s => new Run(s){Foreground = Brushes.Red}},
{"家乡", s => new Run(s){Foreground = Brushes.Blue, FontSize = 28}},
};
static Inline FormatText(string text)
{
Span span = new Span();
int startIndex = 0;
while (true)
{
var hit = keywords.Keys.Select(k => new { word = k, index = text.IndexOf(k, startIndex) }).OrderBy(x => (uint)x.index).FirstOrDefault();
if(hit.index < 0)
{
span.Inlines.Add(new Run(text.Substring(startIndex)));
break;
}
span.Inlines.Add(new Run(text.Substring(startIndex, hit.index - startIndex)));
span.Inlines.Add(keywords[hit.word](hit.word));
startIndex = hit.index + hit.word.Length;
}
return span;
}
}
测试例子:
xmlns:loc ="clr-namespace:WpfApplication1"这行要根据具体的命名空间改写。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc ="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBox Name="t" Text="张老三,我问你,你的家乡在哪里?"/>
<Separator />
<TextBlock loc:InlineTextBehaviour.InlineText="{Binding Path=Text,ElementName=t}" />
</StackPanel>
</Window>
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
string s = this.tb.Text;
this.tb.Text = string.Empty;
string split = "张三";
while (s.IndexOf(split) >= 0)
{
this.tb.Inlines.Add(new Run(s.Substring(0, s.IndexOf(split))));
this.tb.Inlines.Add(new Run(split) { Foreground = Brushes.Red });
s = s.Substring(s.IndexOf(split) + 2);
}
this.tb.Inlines.Add(new Run(s));
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
string s = this.tb.Text;
this.tb.Text = string.Empty;
string splist = "张三";
this.tb.Inlines.Add(new Run(s.Substring(0, s.IndexOf(splist))));
this.tb.Inlines.Add(new Run(splist) { Foreground = Brushes.Red });
this.tb.Inlines.Add(new Run(s.Substring(s.IndexOf(splist) + 2)));
}
<TextBlock Name="tb" Text="{Binding Name}"></TextBlock>
Student student;
public MainWindow()
{
InitializeComponent();
student = new Student() { Name = "人民张三人民" };
this.DataContext = student;
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
string s = student.Name;
string splist = "张三";
this.tb.Inlines.Add(new Run(s.Substring(0, s.IndexOf(splist))));
this.tb.Inlines.Add(new Run(splist) { Foreground = Brushes.Red });
this.tb.Inlines.Add(new Run(s.Substring(s.IndexOf(splist) + 2)));
student.Name = string.Empty;
}
public class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged("Name");
}
}
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<TextBlock Name="tb"></TextBlock>
string s = "人民张三人民";
string splist = "张三";
this.tb.Inlines.Add(new Run(s.Substring(0, s.IndexOf(splist))));
this.tb.Inlines.Add(new Run(splist) { Foreground = Brushes.Red });
this.tb.Inlines.Add(new Run(s.Substring(s.IndexOf(splist) + 2)));
<TextBlock>
<Run></Run>
<Run></Run>
</TextBlock>
或者写一个转换类,根据内容 来返回字体颜色。