操作无效时的ItemsSource正在使用中。与ItemsControl.ItemsSource访问和修改的元素替代 [英] Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
问题描述
我绑定是新和WPF最近我已经学会了如何创建一个的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>
这就是code:
and this is the code:
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>
和这里是code:
ObservableCollection<Employee> Gemployees;
var employees = new ObservableCollection<Employee>(search.employees());
search.employees()
得到我的DB全体员工的名单
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);
的的ListView
执行刷新每当我添加或删除Gemployees的项目!很酷,但还是有约束力的一个小小的努力。我现在做一个接口类每一个的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
来的财产的DataContext
名为产品
,所以更新集合,你需要去到项目
在属性中的的DataContext
和明确的。
Your are 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.
此外,项目
属性需要是类型的ObservableCollection
,而不是一览
如果你想它来更新UI每当底层集合改变。
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.
您code位的设置的ItemsSource
在code后面是不是需要,应予删除。你只需要设置的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&LT;学生&GT;
,当你想清除列表您可以调用
you are binding the ItemsSource
to the ObservableCollection<Student>
, and when you want to clear the list you can call
Students.Clear()
这篇关于操作无效时的ItemsSource正在使用中。与ItemsControl.ItemsSource访问和修改的元素替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!