简体   繁体   中英

How to bind the source of an image to a directory + name in wpf?

I have a list of image files that I want to create a datagrid based on them and show a thumbnail. The list contains images files related to a path such as follow:

class myclass
{
    public List<string> images;
    public string RootPath;

} 

I need to write a converter to bind to two parameter and then create a thumbnail and the result became the source of images.

I already wrote an converter to create the source of image as follow:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        try
        {
            var bi = new BitmapImage();
            bi.BeginInit();
            bi.DecodePixelWidth = 100;
            bi.CacheOption = BitmapCacheOption.OnLoad;
            bi.UriSource = new Uri(value.ToString());
            bi.EndInit();
            return bi;

        }
        catch
        {
           // do nothing. Maybe return a default image
        }
        return null;
    }

But this converter bind to only one property, but I need to generate a way to bind it to two (or more) values? How can I do this?

You might use a multi-value converter as shown in the following ItemsControl example. It uses a MultiBinding for the Source property of the Image control, where the first binding uses the DataContext of the ItemsControl to access the RootPath property.

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding Images}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Stretch="None">
                <Image.Source>
                    <MultiBinding Converter="{StaticResource ImagePathConverter}">
                        <Binding Path="DataContext.RootPath"
                                 ElementName="itemsControl"/>
                        <Binding/>
                    </MultiBinding>
                </Image.Source>
            </Image>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

The example assumes that the view model class looks like this:

public class ViewModel
{
    public List<string> Images { get; set; }
    public string RootPath { get; set; }
}

The converter could be implemented like this:

public class ImagePathConverter : IMultiValueConverter
{
    public object Convert(
        object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var path = Path.Combine(values.OfType<string>().ToArray());
        var bi = new BitmapImage();
        bi.BeginInit();
        bi.DecodePixelWidth = 100;
        bi.CacheOption = BitmapCacheOption.OnLoad;
        bi.UriSource = new Uri(path);
        bi.EndInit();
        return bi;
    }

    public object[] ConvertBack(
        object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM