wpfimageimagesource

WPF - Image source with MultiBinding


In WPF, How to use Image source with Multi:

<Image x:Name="imgLane1" HorizontalAlignment="Left"  Height="25" Margin="180,225,0,0" VerticalAlignment="Top" Width="42" Stretch="Fill">
                                        <Image.Source>
                                            <MultiBinding Converter="StaticResource LaneImageConverter}" UpdateSourceTrigger="PropertyChanged">
                                                <Binding Path="Attachments">
                                                <Binding Path="Delivery"/>
                                            </MultiBinding>
                                        </Image.Source>
                                    </Image>`
 public class LaneImageConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string path = "";
        int attachments = 0;
        string delivery = values[1].ToString();
        Int32.TryParse(values[0].ToString(), out attachments);
        return System.Windows.Data.Binding.DoNothing;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I can not update imageSource with MultiBinding also debug to update source Please help me ?


Solution

  • The converter must return an instance of a class that is derived from ImageSource, e.g. a BitmapImage that is loaded from the path of an image file:

    public object Convert(
        object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string path = "";
        // compose path from values
    
        return new BitmapImage(new Uri(path));
    }
    

    When the image file is an assembly resource, you would use a Resource File Pack URI:

    var uri = string.Format("pack://application:,,,/images/{0}{1}.png", values); // for example
    
    return new BitmapImage(new Uri(uri));