我们假设我有以下ListView:
<ListView Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Header="IsTrue" DisplayMemberBinding="{Binding Path=IsTrue}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=name}" />
</GridView>
</ListView.View>
</ListView>
以下我要绑定的Test类:
public class Test
{
public Test(Boolean IsTrue, string name)
{
this.IsTrue = IsTrue;
this.name = name;
}
public Boolean IsTrue { get; set; }
public string name { get; private set; }
}
这是我用来添加ListViewItem的命令:
Test a = new Test(false, "a");
listView.Items.Add(a);
现在,当我尝试更改对象IsTrue值时,ListView上的值IsTrue将不会更新。为什么会这样?
答案 0 :(得分:2)
您必须为类Test
实现INotifyPropertyChanged接口,因此当您更改属性时,将通知用户界面。
答案 1 :(得分:2)
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
var e = PropertyChanged;
if (e != null)
e(this, args);
}
private bool isTrue;
public Boolean IsTrue
{
get { return isTrue; }
set
{
if (isTrue == value)
return;
isTrue = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsTrue"));
}
}
public string Name { get; private set; }
public Test(Boolean isTrue, string name)
{
this.isTrue = isTrue;
Name = name;
}
}