0

I'm using an xml as source of my application via binding. in the xml there is a list of folders and a path for a sample image for each folder. The folder list is binded to listbox, and another display is binded to the selected item of the listbox, which is an item of the xml list (type XmlNode). I added the opertunity to add and remove items using the XmlDocument which was copied from the xml by the XmlProvider and save it to the source file.

The problem begins when the source list is empty, either at the application load time, or after removing all of the items. at this point all of the binded values of the display are null. I solved all of the bindings with the binding's TargetNullValue property, exept the canvas background imagebrush image_source property which shows nothing.

I tried to use a converter, but when I debuged it I saw something weird. if there were items in the list the converter returned what is should and the image was displayed, but if the list was empty, the converter returned what it should and no image was shown! plz help me.

Code:

XML:

  <Folders>
    <Folder Id="1">
      <Path>folder3\1</Path>
      <SampleImage>C:\images\2011-09-22\site3\1\6.jpg</SampleImage>
    </Folder>
  </Folders>

XAML:

    <Canvas.Background>
      <ImageBrush x:Name="SampleImage" Stretch="Uniform" >
        <ImageBrush.ImageSource>
          <MultiBinding Converter="{StaticResource ImageConverter}" Mode="OneWay">
            <Binding XPath="./SampleImage" />
            <Binding Source="C:\images\SampleImages\no_image.jpg"/>
          </MultiBinding>
        </ImageBrush.ImageSource>
      </ImageBrush>
   </Canvas.Background>

c#:

public class ImageConverter : IMultiValueConverter
{
    public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ImageSourceConverter imageConverter = new ImageSourceConverter();
        bool bool1=value[0].Equals(DependencyProperty.UnsetValue);
        if (value[0] != null &&!bool1) //if the source isn't null
        {
             //this works fine
             return imageConverter.ConvertFromString(value[0].ToString());
        }
        //here the converter returns the right object but the alternate image isn't shown and the background left blank
        return imageConverter.ConvertFromString(value[1].ToString());

        //here too the converter returns the right object but the alternate image isn't shown and the background left blank
        //return imageConverter.ConvertFromString(@"C:\images\SampleImages\no_image.jpg");

    }


    public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

}
Seffix
  • 1,049
  • 1
  • 12
  • 16
  • This is likely too obvious, but *C:\images\SampleImages\no_image.jpg* is actually there right? And it's not just a blank image? – Kent Boogaart Oct 29 '11 at 19:51
  • The image is there. if I put this path as the Sample image in the XML, the image is shown as it should. – Seffix Oct 29 '11 at 21:19

1 Answers1

0

You call a method on one of the values before checking it for null:

bool bool1=value[0].Equals(DependencyProperty.UnsetValue);
if (value[0] != null &&!bool1)

This is just one possible source for errors. Of course the file paths need to be correct as well as Kent Boogaart already pointed out. Further: "this don't work" is not helpful, if you want good answers provide as much information as possible. i.e. what exactly happened & what you expected and how those expectations were not met.


The converter could be compressed to the following by the way:

string path = (value[0] is string && value[0] != null) ?
    (string)value[0] : (string)value[1];
return new ImageSourceConverter().ConvertFromString(path);

Quite possibly still not ideal but less cluttered.


Edit: As the code works for me i suspect that the layout is to blame, if there are no items your control possibly does not take up any space anymore, hence making the image invisible.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • First, I added informatic comments on the code. second, I tried your compressed code but it has the same result. – Seffix Oct 29 '11 at 21:16
  • Have you tried using another image file or applying the converter on another control? That code works for me, if the first bindng fails it correctly uses the alternate image. – H.B. Oct 29 '11 at 21:51
  • Your edit was the answer! my canvas size was binded to this image size. when the image source was null it retruns 0, so the alternate image was there but the canvas itself was invisible. I added the max size of the canvas as a parameter to the converter and now the image is shown. Thanks! – Seffix Oct 29 '11 at 22:12