WPF,TextBlock的这个字体颜色如何变?

sr534 2015-08-27 03:35:26
WPF的流文档我还没有学习。想问下这个问题:一个textblock1,假如其Text中出现"张三"二字,那么"张三"二字的字体颜色就为红色,其它字符的颜色为默认的黑色。但现在还不知道textblock1的Text值是什么,请问,这个textblock1该怎么写?
...全文
15012 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ouylvr 2017-06-20
  • 打赏
  • 举报
回复
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))); }
wjq 2015-08-28
  • 打赏
  • 举报
回复
改变整个控件字体的颜色:Foreground TextBlock里部分文字颜色改变,往TextBlock里添加多个<Run />每个可以独立有不同属性。
Forty2 2015-08-28
  • 打赏
  • 举报
回复
TextBlock的Inlines属性(就是那些Runs等),不是依赖属性,因此不能直接绑定。 如果要使用数据绑定,可以通过一个Attached Property(附加属性)来做:

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>
sunny906 2015-08-28
  • 打赏
  • 举报
回复 1
sunny906 2015-08-28
  • 打赏
  • 举报
回复
引用 7 楼 sr534 的回复:
[quote=引用 4 楼 sunny906 的回复:]

<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)));
而且,你这个代码的话,如果出现两个"张三",就只有一个是红色的: [/quote]

        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));
        }
sunny906 2015-08-28
  • 打赏
  • 举报
回复
上面的逻辑有点问题,直接这样写吧:

        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)));
        }
sr534 2015-08-28
  • 打赏
  • 举报
回复
引用 4 楼 sunny906 的回复:

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

而且,你这个代码的话,如果出现两个"张三",就只有一个是红色的:
sunny906 2015-08-28
  • 打赏
  • 举报
回复

<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));
            }
        }
    }
sr534 2015-08-28
  • 打赏
  • 举报
回复
引用 4 楼 sunny906 的回复:

<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 的文本使用的是绑定,那么,该如何使用Inlines啊 ??
sunny906 2015-08-28
  • 打赏
  • 举报
回复

<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)));
exception92 2015-08-27
  • 打赏
  • 举报
回复
假如其Text中出现"张三"二字 -》不知道所谓的 ”出现“ 是怎样?如果是赋值,可以把汉字 包含在 Run对象中,并设置其forground属性。类似:

<TextBlock>
<Run></Run>
<Run></Run>

</TextBlock>
或者写一个转换类,根据内容 来返回字体颜色。
E次奥 2015-08-27
  • 打赏
  • 举报
回复
你需要的是RichTextBox控件

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧