gpt4 book ai didi

c# - 绑定(bind)属性无法提供值时的正确行为

转载 作者:行者123 更新时间:2023-11-30 22:55:26 25 4
gpt4 key购买 nike

我有一个包含大量预览图像的 ListBox。图像是从互联网上下载的,并在用户滚动浏览图像时缓存在本地。因此,使用 AsyncPreview_120 绑定(bind)下载图像并返回缓存文件的路径。如果失败,将使用自定义转换器根据文件名生成后备图像。

<Image.Source>
<PriorityBinding FallbackValue="{StaticResource DefaultImage}">
<Binding Path="AsyncPreview_120" IsAsync="True" />
<Binding Path="FileName" Converter="{StaticResource nameToImageSourceConverter}" IsAsync="True" />
</PriorityBinding>
</Image.Source>

在某些情况下,可能会发生下载预览失败的情况,例如由于没有互联网连接。虽然这按预期工作,但我的问题是 Binding 可以返回或抛出什么,这样我就不会从 wpf 收到警告或错误。

我试过:

  • 返回 null
  • 返回空字符串
  • 抛出 NotSupportedException
  • 返回 DependencyProperty.UnsetValue

但都导致了错误或警告。是否有任何其他绑定(bind)行为方式,以便 WPF 将此视为某种“合法”情况并默默地继续前进?还是我可以使用更好的方法?

最佳答案

使用 Binding.DoNothing Binding.DoNothing

A binding source property or a converter can return Binding.DoNothing to instruct the binding engine not to perform any action. For example, to instruct the binding engine not to transfer a value to the binding target, not to move to the next Binding in a PriorityBinding, or not to use the FallBackValue or default value.

关于c# - 绑定(bind)属性无法提供值时的正确行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55256362/

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