Imagebrush image source binding converter

2019-08-17 17:51发布

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

}

1条回答
Anthone
2楼-- · 2019-08-17 18:36

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.

查看更多
登录 后发表回答