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;
}
}