简体   繁体   English

ListView不更新与绑定列表

[英]ListView not updating with bound List

I am trying to implement a simple application : I have a list of objects in my ViewModel i want to display in a ListView with a Datatemplate. 我正在尝试实现一个简单的应用程序:我的ViewModel中有一个对象列表,我想在带有Datatemplate的ListView中显示。 Everything works well with binding until I add a button that adds an element in the list. 在绑定之前,一切工作都很好,直到我添加一个在列表中添加元素的按钮。 When I do that, the ListView doesn't update. 当我这样做时,ListView不会更新。 However, if I try to scroll down on the list, the whole program stops and the following message appears : 但是,如果我尝试向下滚动列表,则整个程序将停止,并显示以下消息:

"The application is in break mode, Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)." “应用程序处于中断模式,您的应用程序已进入中断状态,但是由于所有线程都在执行外部代码(通常是系统代码或框架代码),因此没有任何可显示的代码。”

Here is the code : 这是代码:
Model class: 型号类别:

namespace WpfTest
{
    class Model
    {
        public string Value1 { get; set; }
        public int Value2 { get; set; }
        public bool Value3 { get; set; }

        public Model()
        {
            Value1 = "abc";
            Value2 = 3;
            Value3 = true;
        }
    }
}

ViewModel class : ViewModel类:

namespace WpfTest
{
    class ViewModel : INotifyPropertyChanged
    {
        public List<Model> _model;
        private ICommand _addElement;

        public ViewModel()
        {
            _addElement = new RelayCommand(Add);
            _model = new List<Model>()
            {
                new Model(),
                new Model()
            };
        }

        public List<Model> ModelList
        {
            get => _model;
            set => _model = value;
        }

        public ICommand AddElement
        {
            get => _addElement;
            set => _addElement = value;
        }

        public void Add(object o)
        {
            _model.Add(new Model());
            OnPropertyChanged("ModelList");
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string message)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(message));
            }
        }
    }
}

XAML view : XAML视图:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfTest"
        xmlns:Mod="clr-namespace:WpfTest" 
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type Mod:Model}">
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Value1}"></Label>
                <Label Content="{Binding Value2}"></Label>
                <Label Content="{Binding Value3}"></Label>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <Button Command="{Binding AddElement}">Add</Button>
        <ListView ItemsSource="{Binding ModelList}"></ListView>
    </StackPanel>
</Window>

I am using the latest version of Visual Studio Community 2017 我正在使用最新版本的Visual Studio Community 2017

Thanks in advance for the help 先谢谢您的帮助

Replace List<Model> with ObservableCollection<Model> . List<Model>替换为ObservableCollection<Model> That will correctly notify any bound UI elements when the content of the list is changed. 当列表内容更改时,这将正确通知任何绑定的UI元素。

If you want updates to individual model objects to be relected in the UI then the model class has to implement INotifyPropertyChanged , and the property definitions updated accordingly. 如果要在UI中依赖单个模型对象的更新,则模型类必须实现INotifyPropertyChanged ,并且属性定义也会相应地更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM