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

丶灬走出姿态 提交于 2019-12-08 01:57:46

问题


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


回答1:


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}" />


来源:https://stackoverflow.com/questions/11431612/binding-medialelement-from-the-view-xaml-to-a-viewmodels-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!