6

I am using Telerik Slide View control and it supports a SelectionChanged event

private void radSlideView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{        
    var addedItems = e.AddedItems;
}

e contains the MainViewModel object which contains the FileName property. How do I "extract" the FileName property from e? addedItems is aSystem.Collection.IList type

enter image description here

PutraKg
  • 2,226
  • 3
  • 31
  • 60

1 Answers1

11

You need to cast:

if(e.AddedItems.Length > 0)       // make sure there is at least one item..
{
   MainViewModel firstItem = e.AddedItems[0] as MainViewModel;    // cast..
   if(firstItem != null)                                          // if not null..
   {
       string fileName = firstItem.FileName;                      // get the file name
   }
}
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Is this the most proper way to resolve it? I'd expect some method to getting into a list without as'ing, casting or referencing the zero'th element. It seems just unsafe (although I see the protector again empty collection) and works only if we know the type to cast to (and it can't change or we'll have a poof)... – Konrad Viltersten Jun 29 '15 at 13:07
  • 2
    @Konrad, unfortunately WPF is full of trade-offs like that. It's a trade-off between flexibility and safety. – Mike Dinescu Jun 30 '15 at 03:17