Binding Source-Property of MediaElement to FileInfo

I have a viewmodel class that provides a property MediaFile of type FileInfo and i want to bind the property to the Source property of a MediaElement.

The problem is that, that the Source property of the MediaElement expects an Uri, but i can´t access the FullName property (in a converter defined in the binding) of the FileInfo class, cause this will raise a SecurityException.

With images there is no problem, cause the Image control expects an ImageSource object that i can create in the converter using the stream of the FileInfo instance.

How can i define the binding, so that my MediaElement gets the right source? Or how can i pass in the MediaElement to the converter so that i can call SetSource(Stream) on the MediaElement.

The ViewModel:

public class ViewModel {
  // additional code omitted
  public FileInfo MediaFile {get; set;}
}

The Converter:

public class FileInfoToMediaConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        var file = value as System.IO.FileInfo;
        if (MediaResourceFactory.IsImage(file.Extension)) {
            System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
            image.SetSource(file.OpenRead());
            return image;
        }
        else if (MediaResourceFactory.IsVideo(file.Extension)) {
           // create source for MediaElement
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        throw new NotImplementedException();
    }
}

The Bindings:

    <Image Source="{Binding MediaFile, Converter={StaticResource fileInfoToMediaConverter} }"/>
    <MediaElement Source="{Binding MediaFile, Converter={StaticResource fileInfoToMediaConverter}}/>
9
задан Jehof 8 February 2012 в 03:41
поделиться