0
我想顯示我的窗口右側列出的項目。當我第一次初始化我的窗戶時,一切都很完美。我所做的是,我反序列化一個文件,並得到我的ObservableCollection<Model.Resources>
。列表框中顯示了我列表中的所有項目。 當我進入我的添加資源窗口並添加更多資源時,我將這些對象序列化到一個文件中。當我完成添加時,我打電話給一個名爲refresh()
的方法,它將一些文件反序列化並更新我的ObservableCollection<Model.Resources>
。當我完成後,ListBox不會更新,直到我重新啓動我的程序。列表框在反序列化後不會更新
XAML我的窗口有列表框:
<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」我的名單更新?
我的善良先生,你是對的!我謝謝你! – Riki
@Riki,如果答案對您有幫助,請將其標記爲已接受 – ASh