列表框presenting只有一个项目 [英] ListBox presenting only one item

查看:118
本文介绍了列表框presenting只有一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框和类的字符串。每当用户点击应用程序中的添加按钮,创建一个类的实例,并将其添加到被绑定到了列表框列表。我第一次点击添加按钮,在列表框中显示的第一个项目,但下一次就不会显示两个项目。

I have a ListBox and a class with strings. Each time that a user clicks add button in the application, I create a new instance of the class and add it to the list which is binded to the ListBox. The first time I click the add button, the list box shows the first item, but the next time it doesn't show two items.

XAML - 这是列表框

XAML - this is the ListBox:

<ListBox Name="ListBox_BinsRegion" Height="181" Margin="233,16,6,94" Width="253" Background="Transparent" BorderThickness="1" BorderBrush="Black" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}"/> 

背后的code:

The code behind:

List<Class_ListViewItem> List_ListBoxItems = new List<Class_ListViewItem>();

 private void Button_Add_Click(object sender, RoutedEventArgs e)
    {
        Class_ListViewItem item = new Class_ListViewItem();
        item.WH = this.comboBox_WareHouseBinsRegionDefinition.SelectedItem.ToString();
        item.XXFrom = textBox_XXFrom.Text;
        item.XXTo = textBox_XXTo.Text;
        item.YYFrom = textBox_YYFrom.Text;
        item.YYTo = textBox_YYTO.Text;
        item.Z = textBox_ZFrom.Text;

        List_ListBoxItems.Add(item);

        ListBox_BinsRegion.DataContext = List_ListBoxItems;
    }

在哪里是我的错?

Where is my mistake?

推荐答案

WPF不知道什么时候你的收藏正在发生变化。问题就在这里:

WPF does not know when your collection is changing. The problem is here:

List<Class_ListViewItem> List_ListBoxItems = new List<Class_ListViewItem>();

您需要在列表更改为

ObservableCollection<Class_ListViewItem> List_ListBoxItems = new ObservableCollection<Class_ListViewItem>();

的ObservableCollection(System.Collections.ObjectModel)抛出时,集合更改的事件,使WPF可以更新列表框。

ObservableCollection (System.Collections.ObjectModel) throws an event when the collection is changed, so that WPF can update the listbox.

此外,您还可以删除以下行,或者将它移动到你的控制构造。

Also, you can remove the following line, or move it to the constructor of your control.

ListBox_BinsRegion.DataContext = List_ListBoxItems;

这篇关于列表框presenting只有一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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