【问题标题】:ListBox not updating after deserialization反序列化后列表框不更新
【发布时间】:2017-04-24 18:55:04
【问题描述】:

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

具有 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"/>

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

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

【问题讨论】:

    标签: c# wpf xaml serialization listbox


    【解决方案1】:

    refresh() 方法中你改变了一个字段的值

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

    它不调用OnPropertyChanged("resources");

    你应该使用resources属性

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

    _types_tags 相同

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多