1

我只是想学习 DataTriggers。这个简单的代码有什么问题?它告诉我“源”不是 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 字段是枚举类型并且 SendFailed 和 Sent 是两个有效的枚举值时,使用值“SendFailed”或“Sent”是否正确?

4

1 回答 1

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>
于 2013-01-10T17:50:12.133 回答