简体   繁体   English

WPF数据绑定失败(PropertyChanged没有订阅者)

[英]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. 当我检查poco类中的PropertyChanged事件的订户是否为null且poco类更改后UI不变时,我在绑定后使用WPF 4.5和c#语言。 why it is happening ? 为什么会这样呢? this is what I've done(sry for bad english) my poco object: 这是我所做的(抱歉英语不好)我的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));
            }
        }
    }
}

my XAML : 我的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. 您没有设置DataContext。

    public MainWindow()
    {

            InitializeComponent();
            this.DataContext = AppCurrentStatus;

    }

i would prefer declaring bindings in xaml 我宁愿在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 顺便说一句,如果您绑定到TextBlock-Mode = TwoWay没有任何意义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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