aleshko
aleshko

Reputation: 385

wpf usercontrol data-binding using xaml

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?

Upvotes: 0

Views: 1662

Answers (3)

Rachel
Rachel

Reputation: 132618

I don't see you setting the DataContext anywhere, and I don't think you have an object named MediaPlayerMain in your XAML tree

When you write {Binding ElementName=MediaPlayerMain}, you are telling WPF to set the property equal to the XAML object in the Visual Tree that is named MediaPlayerMain

What you're probably looking for instead is to bind to a Property in the DataContext named MediaPlayerMain, in which case your binding would look like this:

<local:PlayList MediaPlayer="{Binding MediaPlayerMain}" />

But that won't work unless your DataContext is set to an object containing the property MediaPlayerMain, such as setting this.DataContext = this in your Window's constructor.

As an alternative, you can use ElementName in your binding to tell WPF to look up the property on an object in the Visual Tree instead of in the DataContext, such as your Window.

<local:MediaPlayerControlMain x:Name="MainWindow" ...>
    <local:PlayList MediaPlayer="{Binding MediaPlayerMain, ElementName=MainWindow}" />
</local:MediaPlayerControlMain>

This will make your binding look for the property MediaPlayerMain in the XAML element named MainWindow, which is your MediaPlayerControlMain class.

If you're new to WPF's DataBinding, I actually have an article on my blog specifically for beginners that should help you understand better what it is and how it works: What is this "DataContext" you speak of?

Upvotes: 0

yo chauhan
yo chauhan

Reputation: 12315

public partial class MediaPlayerControlMain : Window,INotifyPropertyChanged
    {


        public MediaPlayerControlMain()
        {                
            InitializeComponent();
            MediaPlayerMain = new MediaPlayerMain();
        }
        private MediaPlayerMain mediaPlayerMain;

        public MediaPlayerMain MediaPlayerMain
        {
            get { return mediaPlayerMain; }
            set { mediaPlayerMain = value; Notify("MediaPlayerMain"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));

        }
    }

 "{Binding MediaPlayerMain RelativeSource={RelativeSource AncestorType=Window}}"

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.

Upvotes: 1

Echilon
Echilon

Reputation: 10264

You need to name your root element (the Window/UserControl itself) in the markup. Ie:

<Window x:Name="mediaPlayer"
    ....>
    <TextBlock Text="{Binding SomeProperty,ElementName=mediaPlayer}"

Upvotes: 1

Related Questions