綁定後,我使用WPF 4.5和c#語言,當我檢查我的poco類中的PropertyChanged事件的訂戶時,它的null和UI中的任何內容都不會在poco類更改後發生變化。爲什麼它發生?這是我做了什麼(SRY對於英文不好) 我POCO對象:WPF數據綁定失敗(PropertyChanged沒有訂戶)
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
});
}
}
你在哪兒'MainWindow'構造或之後檢查? – 2014-09-13 15:25:07
我檢查的狀態類: ' 保護無效OnPropertyChanged(字符串propertyName的) { 如果(!=的PropertyChanged NULL){ 的PropertyChanged(這一點,新PropertyChangedEventArgs(propertyName的)); } } ' – alizx 2014-09-13 15:26:18
我懷疑在UI有機會掛鉤事件之前檢查過它。在你的窗口和處理程序中放一個按鈕,並嘗試設置AppCurrentStatus對象的屬性'playState',並查看當時是否事件爲null。 – 2014-09-13 15:31:07