2013-10-18 135 views
0

在这里,在一个文本块更改文本的颜色是我的XAML通过触发

<Window.Resources> 
    <sampleData:MainWindow x:Key="DataSource"/> 
    <DataTemplate x:Key="bobReferencer">      
     <TextBlock Text="{Binding Name}" > 
      <TextBlock.Style> 
       <Style TargetType="TextBlock"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding HasErrors}" Value="true"> 
          //what goes in here? 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </TextBlock.Style> 
     </TextBlock>            
    </DataTemplate>  
</Window.Resources> 

代码隐藏(一个XAML引用)

public class bob 
{ 

    public string Name 
    { 
     get; 
     set; 
    } 

    public bool HasErrors 
    { 
     get; 
     set; 
    } 
} 

基本上我想做的事情是,如果HasErrors是真实的那么我希望名称通过触发器显示为红色。但我的xaml没有正确组建。对此有何建议?我也研究了这个链接,但没有多大帮助。
How can I change the Foreground color of a TextBlock with a Trigger?

回答

3

你几乎有..

 <Style TargetType="TextBlock"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding HasErrors}" Value="true"> 
        <Setter Property="Foreground" Value="Red"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
+0

内基本价值应该是红色。但多数民众赞成在工作:)谢谢格伦我会批准这作为一个有效的答案,一旦我从管理员 – Sike12

+0

@ Sike12哎呀获得许可。我编辑了答案,现在显示红色!谢谢! – gleng

+2

还要确保您的模型类(Bob)实现了[INotifyPropertyChanged](http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged.aspx)并让属性在设置器中引发事件UI知道要更新。 –

2

添加一个设置在DataTrigger

<Setter Property="Foreground" Value="Red"/> 
+0

绝对正确aks81谢谢:) – Sike12

+1

欢迎:) – Sandesh