当绑定到IEnumerable且引用保持不变时,忽略PropertyChanged [英] PropertyChanged is ignored when Binding to IEnumerable where reference stays the same

查看:46
本文介绍了当绑定到IEnumerable且引用保持不变时,忽略PropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个最小的示例来说明绑定问题。
IEnumerable< string> NewReference 得到了预期的更新。
IEnumerable< string> SameReference 未更新,可能是因为引用相同。 Raise( SameReference); 不足以使WPF更新引用。

I created a minimal example to illustrate the binding issue. IEnumerable<string> NewReference gets updated as expected. IEnumerable<string> SameReference is not updated, presumably because the reference is the same. Raise("SameReference"); was not enough to make WPF update the reference.

有什么可以是否可以使WPF框架重引用 SameReference ,即使它具有相同的引用?

Is there something that can be done to make the WPF framework revaluate SameReference even though it has the same reference?

xaml:

<Window x:Class="Stackoverflow.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">
    <Grid Background="Azure">
        <Grid.RowDefinitions>
            <RowDefinition Height="5*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button Content="Update" Margin="5" Grid.Row="1" Grid.ColumnSpan="2" Click="Button_Click" />
        <ListBox ItemsSource="{Binding SameReference}" Margin="5" />
        <ListBox ItemsSource="{Binding NewReference}" Margin="5" Grid.Column="1" />
    </Grid>
</Window>

xaml.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Stackoverflow
{
    public partial class MainWindow : Window , INotifyPropertyChanged
    {
        public List<string> data = new List<string> {  };
        public IEnumerable<string> SameReference { get { return data; } } //this returns a reference to an unchanged object
        public IEnumerable<string> NewReference { get { return new List<string>(data); } } //this returns a reference to a new object
        //ObservableCollection<string> conventional is known but not the point of this question

        public event PropertyChangedEventHandler PropertyChanged;

        private void Raise(string propertyName)
        {
            if(null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public MainWindow()
        {
            this.DataContext = this;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            data.Add("This is data.");
            Raise("SameReference"); //successful notify, ignored values
            Raise("NewReference"); //successful notify, processed values
        }
    }
}


推荐答案

这是故意行为。如果您希望集合更新实施 INotifyCollectionChanged (或使用已经存在的类,例如 ObservableCollection< T> )。

That is intentional behavior. If you want collections to update implement INotifyCollectionChanged (or use a class that already does, like ObservableCollection<T>).

这篇关于当绑定到IEnumerable且引用保持不变时,忽略PropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆