将其内容转换为不同颜色的自定义文本块 [英] Custom textblock that transforms its content to different color

查看:22
本文介绍了将其内容转换为不同颜色的自定义文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将编写一个自定义文本块来拆分其文本内容.它将根据条件使文本具有不同的颜色,并且文本将用逗号分隔.逗号将保持黑色.我不知道如何开始.你能提供帮助吗?

I will write a custom textblock that splits its text content. It will make texts diffent colors depending on conditions and the texts will be separated with commas. Commas will remain black. I don't know how to start. Could you please provide help to start?

提前致谢

推荐答案

下面的用户控件使用一个项目控件来显示具有某种随机颜色的每个标记.

Below user control uses an items control to show each token with some random color.

用法:

 <local:ColorTextBlock Text="abcd,abcde, abc, abcdef, abc, abcde, "/> 

XAML:

<UserControl x:Class="WpfApplication1.ColorTextBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d">

    <UserControl.Resources>
        <local:TextColorConverter x:Key="TextColorConverter" />
    </UserControl.Resources>

    <ItemsControl Name="_itemsControl">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding .}" Foreground="{Binding ., Converter={StaticResource TextColorConverter}}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</UserControl>

背后的代码:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class ColorTextBlock : UserControl
    {
        public ColorTextBlock()
        {
            InitializeComponent();
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty = 
            DependencyProperty.Register("Text", typeof(string), typeof(ColorTextBlock), new UIPropertyMetadata(""));

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if (e.Property.Name == "Text")
            {
                // Below code does not handle text that includes stars. Null check probably needed. 
                _itemsControl.ItemsSource = Text.Replace(",", "*,*").Split('*');
            }
        }
    }

    public class TextColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string text = value as string;
            if (text == ",") return Brushes.Black;

            return new SolidColorBrush() { Color = Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)) };
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private static Random _random = new Random();
    }
}

这篇关于将其内容转换为不同颜色的自定义文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆