0
내 창 오른쪽에 나열된 항목을 표시하려고합니다. 창을 처음 초기화 할 때 모든 것이 완벽하게 작동합니다. 내가하는 일은 파일을 비 직렬화하고 내 ObservableCollection<Model.Resources>
을 얻는 것입니다. 내 목록의 모든 항목이 ListBox에 표시됩니다. Add Resource 창에 들어가고 더 많은 리소스를 추가하면 해당 객체를 파일로 직렬화합니다. 추가가 끝나면 refresh()
이라는 메서드를 호출하여 일부 파일을 비 직렬화하고 업데이트합니다 (ObservableCollection<Model.Resources>
). 필자는 프로그램을 다시 시작할 때까지 ListBox를 업데이트하지 않습니다. 내 창 클래스의목록 상자가 비 직렬화 후 업데이트되지 않습니다.
<ListBox x:Name="itemList" Grid.Column="1" HorizontalAlignment="Stretch" Margin="7,23,6,0" Grid.Row="1" VerticalAlignment="Stretch" Background="#324251" ItemsSource="{Binding Path=resources}" FontSize="16" Foreground="Wheat"/>
관련 코드 :
public partial class GlowingEarth : Window, INotifyPropertyChanged
{
private ObservableCollection<Model.Etiquette> _tags;
private ObservableCollection<Model.Resource> _resources;
private ObservableCollection<Model.Type> _types;
public ObservableCollection<Model.Etiquette> tags
{
get
{
return _tags;
}
set
{
_tags = value;
OnPropertyChanged("tags");
}
}
public ObservableCollection<Model.Resource> resources
{
get
{
return _resources;
}
set
{
_resources = value;
OnPropertyChanged("resources");
}
}
public ObservableCollection<Model.Type> types
{
get
{
return _types;
}
set
{
_types = value;
OnPropertyChanged("types");
}
}
private BinaryFormatter fm;
private FileStream sm = null;
private string path;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public GlowingEarth()
{
InitializeComponent();
tags = new ObservableCollection<Model.Etiquette>();
resources = new ObservableCollection<Model.Resource>();
types = new ObservableCollection<Model.Type>();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
refresh();
}
public void refresh()
{
path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "typeList");
if (File.Exists(path))
{
fm = new BinaryFormatter();
sm = File.OpenRead(path);
_types = (ObservableCollection<Model.Type>)fm.Deserialize(sm);
sm.Dispose();
}
else
{
return;
}
path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tagList");
if (File.Exists(path))
{
fm = new BinaryFormatter();
sm = File.OpenRead(path);
_tags = (ObservableCollection<Model.Etiquette>)fm.Deserialize(sm);
sm.Dispose();
}
else
{
return;
}
path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "reslist");
if (File.Exists(path))
{
fm = new BinaryFormatter();
sm = File.OpenRead(path);
_resources = (ObservableCollection<Model.Resource>)fm.Deserialize(sm);
sm.Dispose();
}
else
{
return;
}
InitializeComponent();
this.DataContext = this;
}
당신이 말해 주시겠습니까, 지구에 무슨 일이 벌어지고, 그 이유는 ISN '목록 상자를 가지고 내 윈도우의
XAML 내 목록 업데이트 중입니까? 당신이 OnPropertyChanged("resources");
를 호출하지 않습니다
_resources = (ObservableCollection<Model.Resource>)fm.Deserialize(sm);
필드의 값을 변경 refresh()
방법