作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的类(class)有一个 bool 属性,比如:
private bool isFolder;
public bool IsFolder{ get; set; }
我在 xaml 中有一个图像:
<Image Source="..">
我希望该图像在 IsFolder 为真时有一个 imageSource,在 IsFolder 为假时有另一个。我该怎么做?
提前致谢。
最佳答案
DataTriggers
仅用于该目的。
在 IsFolder 为假的情况下设置默认值(将其绑定(bind)到 NotFolderImage 属性)。如果 IsFolder 值设置为 true,请将源设置为其他值(本例中为 FolderImage)。
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{Binding NotFolderImage}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsFolder}" Value="True">
<Setter Property="Source" Value="{Binding FolderImage}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
此外,确保底层 ViewModel 类正在实现 INotifyPropertyChanged事件,以便将 IsFolder 属性的更改传播到 UI。
关于wpf 图像源取决于对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24894052/
我是一名优秀的程序员,十分优秀!