为什么字符串INotifyPropertyChanged属性更新但不是列表< string>? [英] Why does a string INotifyPropertyChanged property update but not a List<string>?

查看:189
本文介绍了为什么字符串INotifyPropertyChanged属性更新但不是列表< string>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的WPF应用程序中,当您单击按钮时,为什么 TheTitle TextBlock更新但 FilesCopied ListBox不更新?

In the following WPF application, when you click the button, why does TheTitle TextBlock update but FilesCopied ListBox not update?

XAML:

<Window x:Class="TestList3433.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TextBlock Text="{Binding TheTitle}"/>
        <TextBlock Text="above"/>
        <ListBox ItemsSource="{Binding FilesCopied}"/>
        <TextBlock Text="below"/>

        <Button Content="Add to collection" Click="Button_Click"/>
    </StackPanel>
</Window>

代码隐藏

using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;

namespace TestList3433
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region ViewModelProperty: FilesCopied
        private List<string> _filesCopied = new List<string>();
        public List<string> FilesCopied
        {
            get
            {
                return _filesCopied;
            }

            set
            {
                _filesCopied = value;
                OnPropertyChanged("FilesCopied");
            }
        }
        #endregion

        #region ViewModelProperty: TheTitle
        private string _theTitle;
        public string TheTitle
        {
            get
            {
                return _theTitle;
            }

            set
            {
                _theTitle = value;
                OnPropertyChanged("TheTitle");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            FilesCopied.Add("test1.txt");
            TheTitle = "This is the title";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FilesCopied.Add("test2.txt");
            TheTitle = "title was changed";
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}



答案:



感谢Robert,我忘了ObservableCollection。这是答案:

Answer:

Thanks Robert, I forgot about ObservableCollection. Here is the answer:

将FilesCopied块更改为:

Change the FilesCopied block to this:

#region ViewModelProperty: FilesCopied
private ObservableCollection<string> _filesCopied = new ObservableCollection<string>();
public ObservableCollection<string> FilesCopied
{
    get
    {
        return _filesCopied;
    }

    set
    {
        _filesCopied = value;
        OnPropertyChanged("FilesCopied");
    }
}
#endregion

添加: p>

And add:

using System.Collections.ObjectModel;


推荐答案

因为它不实现IBindingList(View)和因此UI永远不会知道你将任何新内容添加到列表中。

Because it doesn't implement IBindingList(View) and therefor the UI never knew that you put anything new into the list.

使用BindingList或ObservableCollection。

Use a BindingList or ObservableCollection.

这篇关于为什么字符串INotifyPropertyChanged属性更新但不是列表&lt; string&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆