Below the user control, the control is used to display each token with some random color.
Using:
<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>
Code behind:
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") {
Wallstreet programmer
source share