繁体   English   中英

为什么ListBox不绑定到IEnumerable更新?

[英]Why doesn't a ListBox bound to an IEnumerable update?

我有以下XAML:

<Window x:Class="ListBoxTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ListBoxTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:Model />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListBox ItemsSource="{Binding Items}" Grid.Row="0"/>
        <Button Content="Add" Click="Button_Click" Grid.Row="1" Margin="5"/>
    </Grid>
</Window>

以及以下Model类的代码,这些代码放入主窗口的DataContext

public class Model : INotifyPropertyChanged
{
    public Model()
    {
        items = new Dictionary<int, string>();
    }

    public void AddItem()
    {
        items.Add(items.Count, items.Count.ToString());

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Items"));
    }

    private Dictionary<int, string> items;
    public IEnumerable<string> Items { get { return items.Values; } }

    public event PropertyChangedEventHandler PropertyChanged;
}

和主窗口的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var model = this.DataContext as Model;
        model.AddItem();
    }
}

按下按钮时,列表的内容不会被更新。

但是,当我将Items属性的getter更改为此时:

public IEnumerable<string> Items { get { return items.Values.ToList(); } }

它开始工作。

然后,当我注释掉发送PropertyChanged事件的部分时,它将再次停止工作,这表明该事件已正确发送。

因此,如果列表接收到该事件,为什么没有ToList调用就无法在第一个版本中正确更新其内容?

仅当属性值实际更改时,才Items属性的PropertyChanged事件。 引发事件时,WPF绑定基础结构会注意到属性getter返回的集合实例与以前相同,并且不执行任何操作来更新绑定目标。

但是,当您返回items.Values.ToList() ,每次都会创建一个新的集合实例,并且更新绑定目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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