2015-12-20 39 views
0

我我的對象綁定到ListView,這是我GridViewColumn更改WPF GridViewColumn TextBlock的文本顏色爲多種顏色裏面串

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

正如你可以看到物業Message被綁定到我的ListViewColumn。 這Message是不斷變化的,根據我的檔案狀態一個簡單的字符串,這是我所有的消息選項:

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

所以我想要做的是改變​​顏色爲每MessageStarted白色,Finished藍色。 ..)但這裏的前提條件是在Message類型4:

消息格式Delay for x secondsx是數字,通過定時器,此值我想改變​​顏色來這個消息裏面其他東西倒過來數。

例如:

<Span Foreground="White">Delay for</Span> <Span Foreground="Red">x</Span><Span Foreground="White">seconds</Span> 

這可能嗎?

+0

具有u檢查了我的答案嗎? – AnjumSKhan

回答

0

是的,你可以使用ColorAnimation

您還可以搜索「使用WPF教程的ColorAnimation」。

我爲你開發了一個示例應用程序。它演示隨機顏色生成,並使用DispatcherTimer類。我已經使用了TextBox,因爲它在Text更改時引發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));  

    } 
}