-1

I have control where I bind the images. The code in my view model looks as following:

public List<IDocument> SelectedEventPhotoList
{
    get { return _selectedEventPhotoList; }
    set
    {
        if (Equals(value, _selectedEventPhotoList))
            return;

        _selectedEventPhotoList = value;
        RaisePropertyChanged(() => SelectedEventPhotoList);
    }
}

public IDocument SelectedEventPhoto
{
    get { return _selectedEventPhoto; }
    set
    {
        if (Equals(value, _selectedEventPhoto))
            return;

        _selectedEventPhoto = value;
        RaisePropertyChanged(() => SelectedEventPhoto);
    }
}

The binding looks as following:

<ListView Grid.Row="0"
            ItemsSource="{Binding SelectedEventPhotoList, Converter={StaticResource PathToFileConverter}}"
            SelectedItem="{Binding SelectedEventPhoto}"

As you can see I have a list of IDocument types to bind to ItemsSource and SelectedItem is of IDocument type. But, images have Source property that is of type string and I've used PathToFileConverter value converter to convert IDocument types to strings.

The issue is in fact that after using converter, SelectedItem is null.

How can I achieve the SelectedItem keeps IDocument type, which is not null?

tesicg
  • 3,971
  • 16
  • 62
  • 121
  • Why are you converting the ItemsSource to another type? This makes no sense. – mm8 Feb 27 '17 at 10:49
  • Because IDocument is not right type for image's Source property. On the other hand I need the SelectedItem to be IDocument type. – tesicg Feb 27 '17 at 10:57
  • 1
    You should apply the converter to the Source property binding then and not to the ItemsSource of the ListView. – mm8 Feb 27 '17 at 10:58
  • If you write your suggestion as regular answer, I'll mark it as useful. Thanks. – tesicg Feb 27 '17 at 11:17

2 Answers2

0

You should apply the converter to the Source property binding of the Image and not to the ItemsSource of the ListView

The type of the property that is bound to the SelectedItem property of a ListView should always be T if the ItemsSource property is bound or set to an IEnumerable<T>.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

You should remove the converter from the ItemSource binding, and add DisplayMemberPath="PathOfFileProperty", which 'PathOfFileProperty' is the string property on IDocument which indicate the file path

Omri Aviv
  • 126
  • 1
  • 3