WPF:最简单的DataTriggers

时间:2013-01-10 17:42:06

标签: wpf datatrigger

我只是想学习DataTriggers。这个简单的代码有什么问题?它告诉我“Source”不是FrameworkElement的属性。

<Image>
    <Image.Triggers>
        <DataTrigger Binding="{Binding Status}" Value="Sent">
            <Setter Property="Source" Value="Resources\Approve_64_Trans.bmp"/>
        </DataTrigger>

        <DataTrigger Binding="{Binding Status}" Value="SendFailed">
            <Setter Property="Source" Value="Resources\Cancel_64_Trans.bmp"/>
        </DataTrigger>
    </Image.Triggers>
</Image>

此外,如果Status字段为enum类型且SendFailed和Sent是两个有效的枚举值,则使用值“SendFailed”或“Sent”是否正确?

1 个答案:

答案 0 :(得分:2)

你需要将这种触发器放在样式中,如下所示:

  <Image>
     <Image.Style>
        <Style TargetType="Image">
           <Style.Triggers>
              <DataTrigger Binding="{Binding Status}" Value="Sent">
                 <Setter Property="Source" Value="Resources\Approve_64_Trans.bmp"/>
              </DataTrigger>
              <DataTrigger Binding="{Binding Status}" Value="SendFailed">
                 <Setter Property="Source" Value="Resources\Cancel_64_Trans.bmp"/>
              </DataTrigger>
           </Style.Triggers>
        </Style>
     </Image.Style>
  </Image>