1

I have ItemsControl (listbox) with some custom complex data templates. And its take few seconds to show window with this listbox!

My listbox datatemplate contains image:

<Image Stretch="Uniform"
       x:Name="image"
       Grid.ColumnSpan="1"
       Source="{Binding ImagePath}"
       HorizontalAlignment="Stretch"
       VerticalAlignment="Stretch"
       Width="Auto"
       Grid.Column="0"
       MinWidth="20"
       MinHeight="20"
       d:LayoutOverrides="GridBox"
       Margin="1,0,1,1" />

Almost all ImagePaths is null. I noticed that this null generates exception but wpf engine hides it! I think this is the source of problem. Is it possible to disable image loading when i know that image path is null!?

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Ievgen
  • 4,261
  • 7
  • 75
  • 124

2 Answers2

4

Add this DataTrigger to your DataTemplate:

<DataTrigger Binding="{Binding ImagePath}" Value="{x:Null}">
    <Setter TargetName="image" Property="Source" Value="{x:Null}"/>
</DataTrigger>
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • And what this will do? This will set null in case if value null? – Ievgen Mar 26 '13 at 19:18
  • @Evgeny have you tried it? This will prevent the default `TypeConverter` from throwing an exception, which is what is actually causing your delay. – Federico Berasategui Mar 26 '13 at 19:19
  • 1
    Or you could combine the two answers and use `Source={Binding ImagePath, FallbackValue={x:Null}}` and get the same result. – Brent Stewart Mar 26 '13 at 19:20
  • Looks like I spoke to soon. Still throws when you do that, so don't use my idea. – Brent Stewart Mar 26 '13 at 19:23
  • But what if i will bind setter Value={Binding ImagePath} if imagepath==null is false? – Ievgen Mar 26 '13 at 19:50
  • @Evgeny I don't understand your question. Have you tried my example? – Federico Berasategui Mar 26 '13 at 19:51
  • @HighCore i am testing it. – Ievgen Mar 26 '13 at 19:57
  • @HighCore Just was tested. Same problem. Each image generates exception: – Ievgen Mar 26 '13 at 21:21
  • ImageSourceConverter cannot convert from System.String. at System.ComponentModel.TypeConverter.GetConvertFromException(Object value) – Ievgen Mar 26 '13 at 21:21
  • @HighCore and this is expected because there is no difference how you will pass null value :( – Ievgen Mar 26 '13 at 21:22
  • @Evgeny it works in my sample application. Post your current XAML and binding source. – Federico Berasategui Mar 26 '13 at 21:25
  • 1
    @Evgeny "and this is expected because there is no difference how you will pass null value": that's not true. When you bind Image.Source to a property of type string, WPF automatically performs a type conversion from `string` to `ImageSource` by a built-in TypeConverter. This conversion is bypassed when you apply the DataTrigger as shown in this answer. You may however apply your own [binding converter](http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter.aspx) for the type conversion. – Clemens Mar 26 '13 at 22:04
1

Try FallBackValue attribute which sets a value when binding exception is thrown (msdn link) eg: FallbackValue="pack://application:,,,/Resources/Images/nocover.png" or {x:null}

There is also TargetNullValue which sets a default value when the bound data is null

Another option would be trying with a converter which returns a default value if parameter is null..

Resolved similar issue here

Community
  • 1
  • 1
Antoine
  • 11
  • 2