0

I have a Listbox bound to an ObservableCollection of ImageMetadata class. Item template of Listbox is defined as

<Image Source="{Binding Converter={StaticResource ImageConverter}}" />

ImageConverter is written as

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var metadata = (ImageMetadata)value;
        if (metadata.IsPublic)
        {
            //code to return the image from path
        }
        else
        {
            //return default image
        }
     }

ImageMetadata is the 'Model' class written as

class ImageMetadata : INotifyPropertyChanged
{
    public string ImagePath
    {
        ......
    }

    public bool IsPublic
    {
        ......
    }
}

When an image is updated, I will trigger PropertyChanged event as given below

NotifyPropertyChanged("ImagePath");

Problem here is that : NotifyPropertyChanged event will not work since I am specifying the changed property name as 'ImagePath' and the binding is to 'ImageMetadata' object rather than 'ImagePath' property.

I cannot use

<Image Source="{Binding ImagePath, Converter={StaticResource ImageConverter}}" />

since I need the IsPublic property also to decide which image to display.

How can I modify the code to properly fire PropertyChanged event?

Edit : I am developing for Windows phone 8.

Arctic
  • 807
  • 10
  • 22
  • 2
    There was a [similar question](http://stackoverflow.com/questions/2670606/wpf-update-binding-when-bound-directly-to-datacontext-w-converter) before. See if it helps. – icebat May 07 '14 at 14:12
  • What about `Style` which is Setting your `Source` Property, and inside of the Style you could use `DataTriggers` to define what happens when `ImagePath` has changed. – XAMlMAX May 07 '14 at 14:17
  • You might also go the MVVM way and add a view model class (perhaps derived from your `ImageMetadata` model class), which provides the image by means of another property. – Clemens May 07 '14 at 16:27
  • @Clemens, Can you please explain? Also did you look at icebat's comment on top? Are you recommending something similar? – Arctic May 07 '14 at 19:16
  • You could just add another property which returns the image in a way similar to how your converter does it. You may however not want to have this property in your model class, so you would put it in an "intermediate" class, the so-called view model. You may search the web for MVVM to get more information about this pattern, which has become the standard architectural pattern in WPF, Silverlight and Windows Store applications. – Clemens May 07 '14 at 20:39

1 Answers1

2

You could use a MultiBinding with a multi-value converter:

<Image>
    <Image.Source>
        <MultiBinding Converter="{StaticResource ImageConverter}">
            <Binding Path="ImagePath"/>
            <Binding Path="IsPublic"/>
        </MultiBinding>
    </Image.Source>
</Image>

The Convert method would look like this:

public object Convert(
     object[] values, Type targetType, object parameter,CultureInfo culture)
{
    object result = null;

    if (values.Length == 2 && values[0] is string && values[1] is bool)
    {
        var imagePath = (string)values[0];
        var isPublic = (bool)values[1];
        ...
    }

    return result;
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • That won't work - it is Windows Phone - [more information](http://stackoverflow.com/a/4968247/2681948) – Romasz May 07 '14 at 14:31
  • Thank you, your answer is informative. But multibinding wouldn't help me, since Windows phone development doesn't support it. I am sorry, I should have written it clearly in the question. – Arctic May 07 '14 at 14:32
  • 1
    @Fadi Then you should perhaps remove the `WPF` tag from your question. – Clemens May 07 '14 at 14:35