我在绑定后使用WPF 4.5和c#语言,当我在poco类中检查订阅者的PropertyChanged事件为空时,在poco类更改后UI中没有任何更改。为什么会这样?这就是我所做的(对于糟糕的英语而言) 我的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
});
}
}
答案 0 :(得分:0)
您没有设置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是没有意义的