I have wfp form like that:
public partial class MediaPlayerControlMain : Window
{
MediaPlayerMain MediaPlayerMain;
public MediaPlayerControlMain()
{
MediaPlayerMain = new MediaPlayerMain();
InitializeComponent();
}
}
I have my user control (PlayList) that use MediaPlayerMain object.
That User Control have that:
public partial class PlayList : UserControl
{
public MediaPlayerMain MediaPlayer
{
get { return (MediaPlayerMain)GetValue(MediaPlayerProperty); }
set { SetValue(MediaPlayerProperty, value); }
}
public static readonly DependencyProperty MediaPlayerProperty =
DependencyProperty.Register(
"MediaPlayer", typeof(MediaPlayerMain), typeof(PlayList),
new FrameworkPropertyMetadata()
);
}
Is there the way to set MediaPlayer property using just xaml. I tried to use “{Binding ElementName=MediaPlayerMain}” but it seems to be that MediaPlayerMain haven’t initialized yet. Although i initialized it before InitializeComponent() function. What am i doing wrong?. And what is the best option to pass this object to my user control?
The issue is you are trying to bind the field not property.For binding source must be the property not field because binding system uses reflection and looks only for properties not for fields.I hope this will help.