在wpf中在运行时清除ListBox项 [英] In wpf Clear ListBox items at Runtime

查看:223
本文介绍了在wpf中在运行时清除ListBox项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wpf listbox,我在调用重载数据功能时无法清除列表,我只想在运行时重载新数据,而页面加载时会在我刷新新数据时正确地加载数据是在itemsource中获取的,我可以在调试模式下看到它,但是列表框中没有新数据,旧数据仍保留在列表中,当我调用list.items.clear()时,我什至无法清除,我尝试了很多方法,是否有XAML绑定中的问题,以下是我的代码.

I am using wpf listbox, i cannot able to clear the list when am calling the reload data function, i just want to reload new data at runtime,while page loading it loads the data correctly, when i refresh the new data is fetched in itemsource i can see that in debug mode, but no new data in listbox, old data remains in the list, i cant even clear, when i call list.items.clear(), i tried lot ways, is there any problem in my XAML binding, the following is my code.

XAML:

 <ListBox  ItemsSource="{Binding}"   HorizontalContentAlignment="Left" x:Name="lstbxindex"  Foreground="White" FontSize="20px" Height="400" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" >
            <ListBox.ItemTemplate>    
                <DataTemplate>

                    <WrapPanel Orientation="Horizontal" Margin="5" >

                        <StackPanel Orientation="Horizontal">
                            <TextBlock x:Name="txtblckroundhour" Height="40px" Width="55px" Text="{Binding RoundedHours}"  FontSize="14" Background="#555555" Loaded="txtblckroundhour_Loaded"  Foreground="White"></TextBlock>

                            <Label x:Name="items" MouseDoubleClick="items_MouseDoubleClick" Content="{Binding ProjectRow.Name}" Background="#555555" FontSize="20" Loaded="items_Loaded" Visibility="Visible"  Margin="35,0,0,0"  Width="230" Foreground="White"></Label>
                        </StackPanel>


                        <StackPanel Orientation="Vertical">
                            <ComboBox  Height="40px" Width="290" Margin="-230,0,0,0" Loaded="ComboBox_Loaded" Visibility="Hidden"  IsEditable="True"  FontSize="20"  Background="White"  Foreground="Black"></ComboBox>
                        </StackPanel>

                        <!--<ComboBox  x:Name="ComboBox_AddItem"  Height="40px" Width="290" Margin="-35,35,0,0"  Loaded="ComboBox_AddItem_Loaded"  IsEditable="True"  FontSize="20"  Background="White" Visibility="Hidden" Foreground="Black"></ComboBox>-->
                    </WrapPanel>
                </DataTemplate>



            </ListBox.ItemTemplate>
        </ListBox>

获取值列表

private List<ProjectInformation> projectInformationList1 = new List<ProjectInformation>();

//在这里定义用户界面列表框到对象的内存列表的实际绑定.

// Here define the actual binding of the userinterface listbox to the in-memory list of objects.

 foreach (DtoProjectsRow row in projectsTable.Rows)
                {

                    projectInformationList1.Add(new ProjectInformation(row));
                }

  lstbxindex.DataContext = projectInformationList1;

在SO中,我尝试了一些解决方案,但不幸的是,它不适用于我.最后我尝试过,

In SO I tried some solution but unfortunately it is not work for me. Last I tried,

XAML.cs页面

public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<String>), typeof(Window));

        public ObservableCollection<String> MyList
        {
            get
            {
                return (ObservableCollection<String>)GetValue(MyListProperty);
            }
            set
            {
                SetValue(MyListProperty, value);
            }
        }

XAML:

 <ListBox  ItemsSource="{Binding **ElementName=Window**}"   HorizontalContentAlignment="Left" x:Name="lstbxindex"  Foreground="White" FontSize="20px" Height="400" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" >

使用上述解决方案可以清除列表项,但是在页面加载时可以清除列表框,但是我不想清除iistboxitems,在更新用户的值后,它将在列表框中重新加载更新后的值.

Using this above solution listitems are clear but when pageloading the listboxitems are clear but I don't want to clear the iistboxitems, after updating the values from user it will reload the updated value in listbox.

lstbxindex.ItemsSource = null;

lstbxindex.ItemsSource = null;

但是它不起作用.对于页面加载列表框加载了所有项目,每隔15分钟,它将第一次调用load函数,它将重新加载更新后的值,但是第二次它将重新加载更新后的值,并且先前的值再次保留在列表框中. /p>

But its not work.For pageload listbox loaded all items,every 15 min interval it will call the load function for firsttime it will reload the updatedvalues but second time it will reload the updated values and previous values remains in listbox again.

推荐答案

您应该有一个带有集合属性的视图模型类,例如像这样:

You should have a view model class with a collection property, e.g. like this:

public class ViewModel
{
    public ObservableCollection<ProjectInformation> Projects { get; }
        = new ObservableCollection<ProjectInformation>();
}

像这样在XAML中设置窗口或页面的DataContext:

Set the DataContext of your Window or Page in XAML like this:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

并像这样绑定ListBox:

and bind the ListBox like this:

<ListBox ItemsSource="{Binding Projects}">
    ...
</ListBox>

要清除源集合中的所有项目,请使用后面的代码访问DataContext:

To clear all items in the source collection, access the DataContext in code behind:

var vm = (ViewModel)DataContext;
vm.Projects.Clear();


您甚至可以在初始化页面或窗口之前,也可以在后面的代码中代替在XAML中分配DataContext:


Instead of assigning the DataContext in XAML, you may as well do it in code behind, even before the Page or Window is initialized:

public MainWindow()
{
    DataContext = new ViewModel();
    InitializeComponent();
}

这篇关于在wpf中在运行时清除ListBox项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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