[英]Updating ListBox with changes in item property in BindlingList
當我更改 BindingList 中項目的屬性時,盡管基礎 object 已更改,但更改不會傳播到 ListBox。
public class MyClass : INotifyPropertyChanged
{
public override string ToString()
{
return this.Name;
}
#region Name
private string name;
public string Name
{
get
{
return this.name;
}
set
{
if (value == this.name) return;
this.name = value;
this.OnPropertyChanged("Name");
}
}
#endregion
#region INotifyPropertyChanged event
///<summary>
///Occurs when a property value changes.
///</summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for
/// a given property.
/// </summary>
/// <param name="propertyName">The name of the changed property.</param>
protected void OnPropertyChanged(string propertyName)
{
//validate the property name in debug builds
VerifyProperty(propertyName);
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Verifies whether the current class provides a property with a given
/// name. This method is only invoked in debug builds, and results in
/// a runtime exception if the <see cref="OnPropertyChanged"/> method
/// is being invoked with an invalid property name. This may happen if
/// a property's name was changed but not the parameter of the property's
/// invocation of <see cref="OnPropertyChanged"/>.
/// </summary>
/// <param name="propertyName">The name of the changed property.</param>
[Conditional("DEBUG")]
private void VerifyProperty(string propertyName)
{
Type type = this.GetType();
//look for a *public* property with the specified name
PropertyInfo pi = type.GetProperty(propertyName);
if (pi == null)
{
//there is no matching property - notify the developer
string msg = "OnPropertyChanged was invoked with invalid property name {0}: ";
msg += "{0} is not a public property of {1}.";
msg = String.Format(msg, propertyName, type.FullName);
Debug.Fail(msg);
}
}
#endregion
}
和 XAML
<Window x:Class="testBL.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ListBox x:Name="myListBox">
</ListBox>
<Button x:Name="changeButton" Click="changeButton_Click">Change an item</Button>
</StackPanel>
</Window>
因為您將ListBox
綁定到集合,所以每個ListBoxItem
的默認數據上下文是實例。 例如,集合是ObservableCollection<MyClass>
,每個ListBoxItem
的數據上下文是MyClass
實例。
由於您沒有提供數據模板,因此ListBoxItem
有效地綁定到"{Binding}"
,這會導致調用您的MyClass.ToString()
方法。 由於ToString()
不是屬性,因此它不支持屬性更改通知。 以這種方式使用綁定(綁定到ToString()
)僅適用於不可變對象。
解決方案是為您的ListBoxItem
提供顯式綁定,如下所示:
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
或使您的MyClass
object 不可變並替換它們而不是改變它們。
您需要為 BindingList 的 NotifyPropertyChanged 中的每個項目分配一個事件處理程序,並且當發生這種情況時,在事件處理程序中引發一個 CollectionChanged 事件。
我建議使用從 ObservableCollection 繼承並自動分配和刪除處理程序的集合。 我稱其為 VeryObservableCollection。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.