将MedialElement从视图(xaml)绑定到ViewModel的属性 [英] Binding MedialElement from the view (xaml) to a ViewModel's property
问题描述
我正在尝试将Mainl.xaml的medialElement绑定到ViewModel的属性. 在MainViewModel.cs中,我们会找到
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
我想知道要在MainView.xaml中放入什么来进行绑定.
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}" />`
但是我应该对MediaElement做什么? 我现在这样做:
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}" >
` 非常感谢您的回答!对不起我的英语不好.我也是WPF的新手
` Thanks a lot for your answer !! Sorry for my english. i'm also new in WPF
推荐答案
您应该公开要显示的媒体源的Uri
而不是MediaElement
:
You should either expose Uri
of media source you want to show instead of MediaElement
:
public Uri MediaSource { get { /* ... */ } set { /* ... */ } }
<MediaElement Source="{Binding MediaSource}" />
或使用ContentControl
(或ContentPresenter
)显示MediaElement
本身:
or use ContentControl
(or ContentPresenter
) to show MediaElement
itself:
<ContentControl Content="{Binding Media}" />
这篇关于将MedialElement从视图(xaml)绑定到ViewModel的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!