图像源上的 wpf 数据触发器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2414358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf datatrigger on an image source
提问by Berryl
Assuming the binding is right and the image files are where they shuld be, can anyone spot why the image in the xaml below won't change when the trigger evaluates to true?
假设绑定是正确的并且图像文件在它们应该在的地方,那么有人能发现为什么当触发器评估为真时下面的 xaml 中的图像不会改变吗?
Cheers,
Berryl
干杯,
贝瑞尔
<Image Source="..\..\Images\OK.png" Grid.Column="2" Stretch="None">
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding TimeSheet, Converter={StaticResource overQuotaConv}}" Value="True">
<Setter Property="Image.Source" Value="..\..\Images\Error.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
回答by Josh
Try the following...
尝试以下...
- Set the TargetType="{x:Type Image}" on the Style
- Change the setter's Property to Property="Source"
- 在样式上设置 TargetType="{x:Type Image}"
- 将 setter 的属性更改为 Property="Source"
The way your property path is currently defined, it's looking for a property on Image called Image (which doesn't exist) then within that something called Source.
当前定义您的属性路径的方式是,它在 Image 上查找名为 Image(不存在)的属性,然后在名为 Source 的内容中查找属性。
Additionally, remove the Source attribute from the Image tag at the top. This will override whatever is applied by the style. You can move it to another DataTrigger or you can put it in a normal setter like so:
此外,从顶部的 Image 标记中删除 Source 属性。这将覆盖样式应用的任何内容。你可以将它移动到另一个 DataTrigger 或者你可以像这样把它放在一个普通的 setter 中:
<Image Grid.Column="2" Stretch="None">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="..\..\Images\OK.png" />
<Style.Triggers>
<DataTrigger Value="True" Binding="{Binding TimeSheet, Converter={StaticResource overQuotaConv}}">
<Setter Property="Source" Value="..\..\Images\Error.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Your timing is interesting. I am just about to post something on my blog about a control that does something very similar to this but in a much more concise syntax.
你的时机很有趣。我正准备在我的博客上发布一些关于控件的内容,该控件执行的操作与此非常相似,但语法更简洁。