Slim Aloui
Slim Aloui

Reputation: 480

Binding MedialElement from the view (xaml) to a ViewModel's property

I'm trying to make a binding of a medialElement from MainView.xaml to a ViewModel's proprety. in MainViewModel.cs we would find

#region Media
private MediaElement media;
    public MediaElement Media
    {
        get
        {
            return media;
        }
        set
        {
            if (value == media)
                return;
            media = value;
            OnPropertyChanged("Media");
        }
    }
    #endregion

I would like to know what to put in the MainView.xaml to do the binding.

I know that if it were a TextBox I would write

`<TextBox Text="{Binding BGToSet, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />`

But what should i do for MediaElement ? i now do this :

`<MediaElement  VerticalAlignment="Center" Width="700" LoadedBehavior="Manual" Height="450" Stretch="Fill"  MediaOpened="{ Binding mediaMediaOpenedCommand}" >

` Thanks a lot for your answer !! Sorry for my english. i'm also new in WPF

Upvotes: 2

Views: 4876

Answers (1)

max
max

Reputation: 34407

You should either expose Uri of media source you want to show instead of MediaElement:

public Uri MediaSource { get { /* ... */ } set { /* ... */ } }

<MediaElement Source="{Binding MediaSource}" />

or use ContentControl (or ContentPresenter) to show MediaElement itself:

<ContentControl Content="{Binding Media}" />

Upvotes: 6

Related Questions