繁体   English   中英

使用 BindlingList 中项属性的更改更新 ListBox

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM