使用 ItemsSource 时操作无效.使用 ItemsControl.ItemsSource 访问和修改元素 [英] Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
问题描述
我最近刚接触 Binding 和 WPF 我学会了如何使用 Binding 技术创建具有多列的 listBox
I am new in Binding and WPF recently I've learned how to create a listBox
with multiple columns using Binding tech
<ListView ItemsSource="{Binding Items}" Margin="306,70,22,17" MouseDoubleClick="listBoxSS_MouseDoubleClick" Name="listBoxSS" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="first_name " Width="100" DisplayMemberBinding="{Binding Path=First_name}" />
<GridViewColumn Header="last_name" Width="100" DisplayMemberBinding="{Binding Path=Last_name}" />
<GridViewColumn Header="phone_number" Width="100" DisplayMemberBinding="{Binding Path=Phones[0]}" />
<GridViewColumn Header="notes" Width="100" DisplayMemberBinding="{Binding Path=Notes}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
这是代码:
List<Student> arr = search.students();
listBoxSS.ItemsSource = arr;
但问题是当我尝试使用添加或删除项目或清除
but the problem was when I tried to use add or remove item or clear
listBoxSS.Items.Clear();
请我需要一个示例来使用项目源或我可以添加或删除项目或清除列表的方式.
Please I need an example for using the items source or the way that I can ADD or Remove Item or Clear the list.
<ListView ItemsSource="{Binding Items}" Margin="306,70,22,17" MouseDoubleClick="listBoxSS_MouseDoubleClick" Name="listBoxSS" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="first_name " Width="100" DisplayMemberBinding="{Binding Path=First_name}" />
<GridViewColumn Header="last_name" Width="100" DisplayMemberBinding="{Binding Path=Last_name}" />
<GridViewColumn Header="phone_number" Width="100" DisplayMemberBinding="{Binding Path=Phones[0]}" />
<GridViewColumn Header="notes" Width="100" DisplayMemberBinding="{Binding Path=Notes}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
这是代码:
ObservableCollection<Employee> Gemployees;
var employees = new ObservableCollection<Employee>(search.employees());
search.employees()
获取我数据库中所有员工的列表
search.employees()
get the list of all employees in my DB
listBoxPE.ItemsSource = employees;
Gemployees = employees;
现在我可以对 Gemployees 执行所有方法
now I can perform all the methods on Gemployees
Gemployees.Remove((Student)listBoxSS.SelectedItem);
Gemployees.Add((Student)listBoxSS.SelectedItem);
每当我从 Gemployees 添加或删除项目时,ListView
都会执行刷新!!很酷,但在绑定方面仍然有点困难.现在我正在为每个 ListView 做一个接口类,这样我就可以把我的东西放进去.它不会在添加项目方面表现出任何灵活性.
The ListView
perform a refresh whenever I add or remove an Item from Gemployees!! Cool but still a little hard work on binding. Now I am doing an interface class to every ListView so I can put my stuff into it. It wont perform any flexibility in Adding Items.
推荐答案
您正在将 ItemsSource
绑定到名为 Items
的 DataContext
属性code>,所以要更新集合,你需要去DataContext
中的Items
属性并清除它.
You're binding the ItemsSource
to a property in the DataContext
called Items
, so to update the collection, you need to go to the Items
property in the DataContext
and clear it.
此外,Items
属性需要是 ObservableCollection
类型,而不是 List
集合更改.
In addition, the Items
property needs to be of type ObservableCollection
, not List
if you want it to update the UI whenever the underlying collection changes.
您在后面的代码中设置 ItemsSource
的代码是不需要的,应该删除.您只需要在一处设置 ItemsSource
,而不是同时设置.
Your bit of code that sets the ItemsSource
in the code behind is not needed and should be removed. You only need to set the ItemsSource
in one place, not both.
这里有一个简单的例子来说明它是如何工作的:
Here's a simple example of how it can work:
// Using Students instead of Items for the PropertyName to clarify
public ObservableCollection<Student> Students { get; set; }
public MyConstructor()
{
...
Students = search.students();
listBoxSS.DataContext = this;
}
现在当你有:
<ListView ItemsSource="{Binding Students}" ... />
您正在将 ItemsSource
绑定到 ObservableCollection
,当您想清除列表时,您可以调用:
you're binding the ItemsSource
to the ObservableCollection<Student>
, and when you want to clear the list you can call:
Students.Clear()
这篇关于使用 ItemsSource 时操作无效.使用 ItemsControl.ItemsSource 访问和修改元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!