从视图(XAML)的视图模型的属性绑定MedialElement(Binding MedialEle

2019-09-19 07:57发布

我试图让从MainView.xaml到一个视图模型的proprety一个medialElement的结合。 在MainViewModel.cs我们会发现

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

我想知道要放什么东西在MainView.xaml做结合。

我知道,如果它是一个文本框我会写

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

但是,我应该怎么为MediaElement的呢? 我现在这样做:

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

`非常感谢您的回答! 对不起我的英语不好。 我也是新的WPF

Answer 1:

您应该公开Uri媒体源的要代替展现MediaElement

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

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

或使用ContentControl (或ContentPresenter )显示MediaElement本身:

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


文章来源: Binding MedialElement from the view (xaml) to a ViewModel's property