gpt4 book ai didi

c# - 处理 WPF 中的无效图像源

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:12 25 4
gpt4 key购买 nike

我有一个绑定(bind)到 viewmodel 中的字符串资源的 View ,它可以很好地处理指向 Web 上的图像或硬盘图像文件的链接。但是,如果选择了错误的图像源,则由于显而易见的原因,图像不会显示。有没有我可以做的绑定(bind)来查看图像是否有有效的来源,或者如果来源不好,是否有办法放置默认图像。

ViewModel 中的属性:

    public string ImageSource
{
get { return imageSource; }
set
{
imageSource = value;
NotifyPropertyChange("ImageSource");
}
}

Xaml:

<Image Grid.Row="0" Name="picture" Source="{Binding ImageSource}" Height="auto" Width="auto" MaxWidth="750" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  />

绑定(bind)工作正常。

最佳答案

如果保证 ImageSource 对于坏图像为空,那么您可以使用数据触发器:

<Image Grid.Row="0" Name="picture" Height="auto" Width="auto" MaxWidth="750" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding ImageSource}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ImageSource}" Value="{x:Null}">
<Setter Property="Source" Value="default_image.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>

关于c# - 处理 WPF 中的无效图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32309533/

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