DataTrigger In WPF

  今天有人问我关于datatrigger的问题,突然发现之前我居然都没有用过,罪过罪过。

  Trigger是最普遍常用的触发器,它只能检测依赖项属性的变化。当你需要检测一个系统变量或者是你自定义的属性的变化时,就需要用到datatrigger,它的触发源使用绑定机制,例如:

  <Style x:Key="textboxstyle" TargetType="TextBox">

    <Style.Triggers>

       <DataTrigger Binding="{Binding Source={x:Static SystemColors.WindowTextColor}}" Value="#FF000000">

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

       </DataTrigger>

    </Style.Triggers>

  </Style>

   同样,它可以绑定指定ElementName的任意属性,如textblock的text、Label的background等等。

 

posted @ 2012-08-16 13:10  While蹒跚学步...  阅读(835)  评论(1编辑  收藏  举报