Jan Gassen
Jan Gassen

Reputation: 3534

Correct behaviour if bound property cannot provide a value

I have a ListBox that contains a large list of preview images. Images are downloaded from the internet and cached locally as the user scrolls over them. Therefor, the AsyncPreview_120 binding is used which downloads the image and returns the path of the cached file. If this fails, a custom converter is used to generate a fallback image based on the filename.

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

In some cases it might happen that downloading the preview fails, e.g. due to no internet connection. While this is working as expected, my question is what the Binding can return or throw, so that I do not get a warning or error from wpf.

I tried:

but all resulted in errors or warnings. Is there any other way the binding could behave so that WPF treats this as some "legit" situation and silently moves on? Or is there maybe any better approach that I could use?

Upvotes: 2

Views: 48

Answers (1)

Rekshino
Rekshino

Reputation: 7325

Use a 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.

Upvotes: 2

Related Questions