反序列化后ListBox不更新

时间:2017-04-24 18:55:04

标签: c# wpf xaml serialization listbox

我正在尝试显示我窗口右侧列出的项目。当我第一次初始化窗口时,一切都很完美。我所做的是,我反序列化一个文件并获取我的ObservableCollection<Model.Resources>。我列表中的所有项目都显示在ListBox中。 当我进入“添加资源”窗口并添加更多资源时,我将这些对象序列化为文件。当我完成添加后,我调用了一个名为refresh()的方法,该方法反序列化了一些文件并更新了我的ObservableCollection<Model.Resources>。完成后,ListBox不会更新,直到我重新启动程序。

我的Window的XAML有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"/>

我的Window类的相关代码:

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;
    }

你能告诉我,到底发生了什么,为什么我的清单没有更新?

1 个答案:

答案 0 :(得分:2)

refresh()方法中,您可以更改字段的值

_resources = (ObservableCollection<Model.Resource>)fm.Deserialize(sm);

它不会调用OnPropertyChanged("resources");

您应该使用resources 属性

resources = (ObservableCollection<Model.Resource>)fm.Deserialize(sm);

_types_tags

相同