将WPF GridViewColumn textBlock文本颜色更改为字符串

时间:2015-12-20 10:34:18

标签: wpf textblock

我将对象绑定到ListView,这是我的GridViewColumn

<GridViewColumn Width="180" Header="Status" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Message}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

正如您所见,属性Message已绑定到我的ListView Column。 这个Message是一个简单的字符串,根据我的文件状态进行更改,这是我的所有消息选项:

1. Started
2. Finished
3. Stopped
4. Delay for x seconds

所以我想要做的是更改每个foreground Message白色Started蓝色Finished蓝色的Message颜色,但这里的捕捉是Delay for x seconds类型4:

x是通过计时器向后计数的数字时,消息格式为foreground,对于此值,我想将<Span Foreground="White">Delay for</Span> <Span Foreground="Red">x</Span><Span Foreground="White">seconds</Span> 颜色更改为此消息中的其他内容。

例如:

java.lang.NullPointerException
    at e1.E1.start(E1.java:30)
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown....

有可能吗?

1 个答案:

答案 0 :(得分:0)

是的,您可以使用ColorAnimation

您还可以使用WPF教程搜索“ColorAnimation”。

我为您开发了一个示例应用程序。它演示了随机Color生成,它使用DispatcherTimer类。我使用了TextBox,因为当文本被更改时它会引发TextChangedEvent,并且IsReadOnly =“True”。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.Window6"
        Title="Window6" Height="300" Width="300" Background="Black">

    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="106,134,0,0" VerticalAlignment="Top" Width="75" Height="50" Background="Black" Foreground="#FFFDFBFB" BorderBrush="#00EEE416" BorderThickness="0" Click="Button_Click"  />

        <TextBox  IsReadOnly="True"  x:Name="TxtLeft" HorizontalAlignment="Left" Width="30" Margin="201,154,0,0" TextWrapping="Wrap" Text="{Binding TimeLeft}" VerticalAlignment="Top" Background="Black">
            <TextBox.Foreground>
                <SolidColorBrush x:Name="ForeColorKey" Color="AliceBlue"/>
            </TextBox.Foreground>
            <TextBox.Triggers>
                <EventTrigger RoutedEvent="TextBox.TextChanged">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="ForeColorKey" Storyboard.TargetProperty="Color"  To="{Binding ToColor}" Duration="0:0:1"/>
                            </Storyboard>
                        </BeginStoryboard>                
                </EventTrigger>
            </TextBox.Triggers>
        </TextBox>

    </Grid>

</Window>

代码隐藏

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

using System.Windows.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window6.xaml
    /// </summary>
    public partial class Window6 : Window
    {
        ViewModel vm = new ViewModel();

        public Window6()
        {
            InitializeComponent();

            this.DataContext = vm;            
        }

        Random r = new Random(1);
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            vm.TimeLeft = 15;

            DispatcherTimer t = new DispatcherTimer();
            t.Interval = TimeSpan.FromSeconds(1);
            t.Tick += t_Tick;
            t.Start();
        }

        void t_Tick(object sender, EventArgs e)
        {
            --vm.TimeLeft;

            byte[] colors = new byte[3];            
            r.NextBytes(colors);
            vm.ToColor = Color.FromRgb(colors[0], colors[1], colors[2]);
        }
    }

    public class ViewModel : DependencyObject
    {
        public Color ToColor
        {
            get { return (Color)GetValue(ToColorProperty); }
            set { SetValue(ToColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ToColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ToColorProperty =
            DependencyProperty.Register("ToColor", typeof(Color), typeof(ViewModel), new PropertyMetadata(Colors.Red));

        public int TimeLeft
        {
            get { return (int)GetValue(TimeLeftProperty); }
            set { SetValue(TimeLeftProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TimeLeft.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TimeLeftProperty =
            DependencyProperty.Register("TimeLeft", typeof(int), typeof(ViewModel), new PropertyMetadata(15));      

    }
}