[英]WPF data binding fails (no subscriber for PropertyChanged)
當我檢查poco類中的PropertyChanged事件的訂戶是否為null且poco類更改后UI不變時,我在綁定后使用WPF 4.5和c#語言。 為什么會這樣呢? 這是我所做的(抱歉英語不好)我的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沒有任何意義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.