1
바인딩 문제를 설명하기 위해 최소한의 예제를 만들었습니다. IEnumerable<string> NewReference
은 예상대로 업데이트됩니다. 참조가 동일하므로 IEnumerable<string> SameReference
은 업데이트되지 않습니다. Raise("SameReference");
은 WPF에서 참조를 업데이트하기에 충분하지 않았습니다.참조가 동일하게 유지되는 IEnumerable에 바인딩 할 때 PropertyChanged가 무시됩니다.
WPF 프레임 워크가 동일한 참조를 가지고 있더라도 SameReference
을 다시 평가할 수있는 방법이 있습니까?
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
}
}
}