简体   繁体   中英

WPF data binding fails (no subscriber for PropertyChanged)

I use WPF 4.5 and c# language after binding when I check subscribers for PropertyChanged event in my poco class its null and nothing in UI change after the poco class changes. why it is happening ? this is what I've done(sry for bad english) my poco object:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CafeTunes.models
{
    public class Status : INotifyPropertyChanged
    {
        private CurrentMedia CurrentMediaValue;
        public CurrentMedia CurrentMedia
        {
            get { return this.CurrentMediaValue; }
            set
            {
                if (value != this.CurrentMediaValue)
                {
                    this.CurrentMediaValue = value;
                    OnPropertyChanged("CurrentMedia");
                }
            }
        }

        private string playStateValue;

        public string playState
        {
            get { return this.playStateValue; }
            set
            {
                if (value != this.playStateValue)
                {
                    this.playStateValue = value;
                    OnPropertyChanged("playState");
                }
            }
        }

        public  event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

my XAML :

 <TextBlock Name="SongNameText" HorizontalAlignment="Left" Margin="405,71,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="19" Width="85"/>

and this is where i do the binding:

    public partial class MainWindow : MahApps.Metro.Controls.MetroWindow
    {

        public Status AppCurrentStatus = new Status() { playState = "sdfsd", CurrentMedia = new CurrentMedia() {Artist="sdas", SongName="asdas" ,Album="asdasd",FileUrl="asdasd",Format="asdasd"} };

        public MainWindow()
        {

                InitializeComponent();
                SongNameText.SetBinding(TextBlock.TextProperty, new Binding("playState")
                {
                    Source = AppCurrentStatus,
                    Mode = BindingMode.TwoWay
                });

        }
   }

you did not set the DataContext.

    public MainWindow()
    {

            InitializeComponent();
            this.DataContext = AppCurrentStatus;

    }

i would prefer declaring bindings in xaml

 <TextBlock Text="{Binding playState}" HorizontalAlignment="Left" Margin="405,71,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="19" Width="85"/>

btw if you bind to a TextBlock - Mode=TwoWay makes no sense

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM