我只是想学习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”是否正确?
答案 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>