locked
customizing listview, binding to method RRS feed

  • Question

  • I have a List of "Person" objects in my DataContext,  the Person class has a property "Name".

    I want to use a listview to list all the persons like this:

    John <img>
    Peter <img>
    etc..

    where <img> are images with source based on name. I have a method GetImage(string name) in a separate class which does a lookup and returns the corresponding image for each person. 

    How do I do this? 

    I have defined a datatemplate like this:

            <DataTemplate x:Key="listViewTemplate">
                <WrapPanel>
                    <TextBlock Text="{Binding Person.Name}"/>
                    <Image Height="30" Width="30">
                        <Image.Source>
                            <Binding Source="{StaticResource image}">
                            </Binding>
                        </Image.Source>
                    </Image>
                 </WrapPanel>
            </DataTemplate>
            <ObjectDataProvider x:Key="image"
                ObjectType="{x:Type my:ImageCollection}"
                MethodName="GetImage">
                <ObjectDataProvider.MethodParameters>
                    <system:String>Name</system:String>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>


    How do I call the method GetImage so that the listview contains name and image for all persons in the datacontext?

    Wednesday, June 3, 2009 6:49 AM

Answers

  • A few months ago I wrote this code to one of my wpf presentations. It's very simple: it gets a string, appends ".jpg" to it, and looks for this file in the application's directory. If it doesn't work, try to change the UriKind, sometimes it helps, sometimes it does not. :D (Really, I haven't been able to figure out how this UriKind works.)
     [ValueConversion(typeof(string), typeof(ImageSource))]
        public class StringToImageConverter: IValueConverter
        {
    
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, 
                System.Globalization.CultureInfo culture)
            {
                BitmapImage bi = null;
                if (value != null)
                {
                    bi = new BitmapImage();
                    bi.BeginInit();
                    Uri u = new Uri(value + ".jpg", UriKind.Relative);
                    bi.UriSource = u;
                    bi.EndInit();
                }
                return bi;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, 
                System.Globalization.CultureInfo culture)
            {
                return ((BitmapImage)value).BaseUri.AbsolutePath;
            }
    
            #endregion
        }
    
    • Proposed as answer by David Fulop Wednesday, June 3, 2009 7:47 AM
    • Marked as answer by slip99 Wednesday, June 3, 2009 7:59 AM
    Wednesday, June 3, 2009 7:47 AM

All replies

  • I think it would be easier to you to create a Converter (that implements IValueConverter) which gets a string (the person's name), and returns the corresponding BitmapImage. This way you could use exactly the same binding for the TextBlock and the Image. On the latter one you should specify the converter in the binding as a staticresource, and that's all.
    I can provide you some sample code if u like.

    David
    Wednesday, June 3, 2009 7:23 AM
  • Thanks for your reply David. 


    I'll look into creating a converter, but sample code would be appreciated :-)
    Wednesday, June 3, 2009 7:25 AM
  • A few months ago I wrote this code to one of my wpf presentations. It's very simple: it gets a string, appends ".jpg" to it, and looks for this file in the application's directory. If it doesn't work, try to change the UriKind, sometimes it helps, sometimes it does not. :D (Really, I haven't been able to figure out how this UriKind works.)
     [ValueConversion(typeof(string), typeof(ImageSource))]
        public class StringToImageConverter: IValueConverter
        {
    
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, 
                System.Globalization.CultureInfo culture)
            {
                BitmapImage bi = null;
                if (value != null)
                {
                    bi = new BitmapImage();
                    bi.BeginInit();
                    Uri u = new Uri(value + ".jpg", UriKind.Relative);
                    bi.UriSource = u;
                    bi.EndInit();
                }
                return bi;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, 
                System.Globalization.CultureInfo culture)
            {
                return ((BitmapImage)value).BaseUri.AbsolutePath;
            }
    
            #endregion
        }
    
    • Proposed as answer by David Fulop Wednesday, June 3, 2009 7:47 AM
    • Marked as answer by slip99 Wednesday, June 3, 2009 7:59 AM
    Wednesday, June 3, 2009 7:47 AM
  • Thanks David, that did the trick!
    Wednesday, June 3, 2009 7:59 AM