gpt4 book ai didi

wpf 图像源取决于对象属性

转载 作者:行者123 更新时间:2023-12-05 09:22:46 24 4
gpt4 key购买 nike

我的类(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com