Bloque de texto personalizado que transforma su contenido en un color diferente
Frecuentes
Visto 1,322 veces
0
Escribiré un bloque de texto personalizado que divida su contenido de texto. Hará que los textos sean de diferentes colores dependiendo de las condiciones y los textos se separarán con comas. Las comas permanecerán negras. no se como empezar ¿Podría por favor proporcionar ayuda para empezar?
Gracias por adelantado
1 Respuestas
1
A continuación, el control de usuario utiliza un control de elementos para mostrar cada token con un color aleatorio.
Uso:
<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>
Código detrás:
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();
}
}
Respondido el 12 de junio de 12 a las 20:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas wpf wpf-controls textblock or haz tu propia pregunta.
Gracias probaré esta solución. Espero que ayude - desbordamiento