How to substitute a default image when the binding image is null RRS feed

  • Question

  • In my datatemplate I am binding to the Image property from my viewmodel. However, when that Image property is null I would like to substitute a default image. I would also prefer to do this using Xaml. What would I add to the following code to make it do what I want?

    <Image Source="{Binding Image}" Stretch="None">
      <core:IncrementalUpdateBehavior Phase="2"/>
      <core:DataTriggerBehavior Binding="{Binding Image}" ComparisonCondition="NotEqual" Value="{x:Null}"/>


    Monday, January 19, 2015 8:54 AM

All replies

  • Try using a converter to detect if the image is null. If it is, return a default image.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 19, 2015 1:32 PM
  • Sure, I could use a converter, but I was hoping that I could leverage the DataTriggerBehavior. Also, I try to confine converters to cases in which I'm actually converting a value. Here, I'm substituting one image for another. Maybe not much of a distinction, but I thought that DataTriggerBehaviors would be ideal for that.
    Monday, January 19, 2015 10:07 PM
  • The simple way is  setting the control's background by imagebrush (use the default image)

    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Tuesday, January 20, 2015 8:03 PM