WPF数据绑定失败(PropertyChanged没有订阅服务器)



当我在poco类中检查PropertyChanged事件的订阅者时,我在绑定后使用WPF 4.5和c#语言,该事件为null,并且在poco类别更改后UI中没有任何更改。为什么会发生这种事?这就是我所做的(sry表示英语不好)我的poco对象:

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));
            }
        }
    }
}

我的XAML:

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

这就是我进行绑定的地方:

    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
                });
        }
   }

您没有设置DataContext。

    public MainWindow()
    {
            InitializeComponent();
            this.DataContext = AppCurrentStatus;
    }

我更喜欢在xaml 中声明绑定

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

顺便说一句,如果你绑定到一个TextBlock-Mode=TwoWay没有意义

最新更新