I am new in Binding and WPF recently I've learned how to create a listBox
with multiple columns using Binding tech
我是绑定和WPF方面的新手,最近我学习了如何使用绑定技术创建一个包含多个列的列表框
<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>
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.
我需要一个使用项目源的例子,或者我可以添加或删除项目或清除列表的方式。
EDIT:
编辑:
<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>
and here is the code:
这里是代码:
ObservableCollection<Employee> Gemployees;
var employees = new ObservableCollection<Employee>(search.employees());
search.employees()
get the list of all employees in my DB
雇员()得到我的数据库中所有雇员的名单。
listBoxPE.ItemsSource = employees;
Gemployees = employees;
now I can perform all the methods on Gemployees
现在我可以对Gemployees执行所有的方法
Gemployees.Remove((Student)listBoxSS.SelectedItem);
Gemployees.Add((Student)listBoxSS.SelectedItem);
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.
当我从Gemployees添加或删除一个项目时,ListView会执行刷新!!很酷,但是在装订上还是有点困难。现在我正在为每个ListView做一个接口类,这样我就可以把我的东西放进去了。它不会在添加项目时执行任何灵活性。
7 个解决方案
#1
24
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.
您将ItemsSource绑定到DataContext中名为Items的属性,因此要更新集合,需要转到DataContext中的Items属性并清除它。
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.
此外,Items属性需要类型为ObservableCollection,而不是List,如果您希望它在底层集合发生更改时更新UI的话。
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.
不需要设置背后代码中的ItemsSource的代码位,应该删除。您只需要将ItemsSource设置在一个地方,而不是两个地方。
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;
}
Now when you have:
现在当你有:
<ListView ItemsSource="{Binding Students}" ... />
you're binding the ItemsSource
to the ObservableCollection<Student>
, and when you want to clear the list you can call:
您将ItemsSource绑定到ObservableCollection
Students.Clear()
#2
11
Weird but true: the following errant keystrokes in my XAML file produced the error "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.":
奇怪但真实:XAML文件中的以下错误击键产生了错误“操作无效,而ItemsSource正在使用”。使用ItemsControl访问和修改元素。ItemsSource。”:
</ItemsControl.ItemTemplate>x`
Note the "x`" characters after the closing element tag.
注意在结束元素标记之后的“x”字符。
#3
8
I know this question has been answered about 2 years ago, however I had this problem myself as well and thought of a possible solution myself, which works. Maybe this doesn't work in certain scenarios and maybe I'm simply not seeing something, but it worked for me so I'm sharing it here:
我知道这个问题在两年前就已经有了答案,但是我自己也有这个问题,我自己也想到了一个可能的解决办法,这是可行的。也许这在某些情况下不起作用,也许我只是没有看到什么东西,但它对我有用,所以我在这里分享它:
listView.ClearValue(ItemsControl.ItemsSourceProperty);
listView.ItemsSource = NewSource;
I sincerely hope this helps someone.
我真诚地希望这能帮助某人。
#4
3
You need to work against the collection that is data-bound to your ItemsSource
. In order to get collection change notifications (when items get added or removed), you should use an ObservableCollection<T>
:
您需要处理与ItemsSource绑定的数据集。为了获得集合更改通知(当添加或删除项目时),您应该使用一个ObservableCollection
var students = new ObservableCollection<Student>(search.students());
listBoxSS.ItemsSource = students;
students.Clear();
students.Add(new Student("ABC"));
And you should remove the ItemsSource="{Binding Items}"
declaration from your XAML.
您应该从XAML中删除ItemsSource="{绑定项}"声明。
#5
1
Assign the ItemsSource property of the listbox to a public property inside the form's class. Then try adding a removing from that, calling PropertyChanged inside the setter, instead of calling clear directly on the listbox items source.
将列表框的ItemsSource属性分配给表单类中的公共属性。然后尝试添加一个删除,在setter中调用PropertyChanged,而不是直接在listbox项源上调用clear。
#6
1
Late to the party I know but I think this answer isn't quite clear above. Its related to the rogue characters post, but this also causes the exception:
我知道,但我认为这个答案还不太清楚。它与流氓人物的帖子有关,但这也导致了一个例外:
<ItemsControl ItemsSource="{Binding AnObservableCollection}">
<Label Content="{Binding Name}"/>
</ItemsControl>
When what you meant was:
你的意思是:
<ItemsControl ItemsSource="{Binding AnObservableCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Its easy as a newcomer (or before first morning coffee) to think the first is correct and the exception in no way explains what is wrong.
作为一个新人(或者在早上喝咖啡之前),认为第一个是正确的,而这个例外是不可能解释什么是错误的。
#7
0
I had this same problem, and I eventually realized that I was trying to add a new item directly to the control's ItemsSource, rather than to the ObservableCollection that served as the ItemsSource.
我遇到了同样的问题,最终我意识到我试图直接向控件的ItemsSource添加一个新项,而不是作为ItemsSource的ObservableCollection。
I figured I'd post this, as it might help other novice wpfers.
我想我应该把这个贴出来,因为它可能会帮助其他新手。
#1
24
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.
您将ItemsSource绑定到DataContext中名为Items的属性,因此要更新集合,需要转到DataContext中的Items属性并清除它。
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.
此外,Items属性需要类型为ObservableCollection,而不是List,如果您希望它在底层集合发生更改时更新UI的话。
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.
不需要设置背后代码中的ItemsSource的代码位,应该删除。您只需要将ItemsSource设置在一个地方,而不是两个地方。
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;
}
Now when you have:
现在当你有:
<ListView ItemsSource="{Binding Students}" ... />
you're binding the ItemsSource
to the ObservableCollection<Student>
, and when you want to clear the list you can call:
您将ItemsSource绑定到ObservableCollection
Students.Clear()
#2
11
Weird but true: the following errant keystrokes in my XAML file produced the error "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.":
奇怪但真实:XAML文件中的以下错误击键产生了错误“操作无效,而ItemsSource正在使用”。使用ItemsControl访问和修改元素。ItemsSource。”:
</ItemsControl.ItemTemplate>x`
Note the "x`" characters after the closing element tag.
注意在结束元素标记之后的“x”字符。
#3
8
I know this question has been answered about 2 years ago, however I had this problem myself as well and thought of a possible solution myself, which works. Maybe this doesn't work in certain scenarios and maybe I'm simply not seeing something, but it worked for me so I'm sharing it here:
我知道这个问题在两年前就已经有了答案,但是我自己也有这个问题,我自己也想到了一个可能的解决办法,这是可行的。也许这在某些情况下不起作用,也许我只是没有看到什么东西,但它对我有用,所以我在这里分享它:
listView.ClearValue(ItemsControl.ItemsSourceProperty);
listView.ItemsSource = NewSource;
I sincerely hope this helps someone.
我真诚地希望这能帮助某人。
#4
3
You need to work against the collection that is data-bound to your ItemsSource
. In order to get collection change notifications (when items get added or removed), you should use an ObservableCollection<T>
:
您需要处理与ItemsSource绑定的数据集。为了获得集合更改通知(当添加或删除项目时),您应该使用一个ObservableCollection
var students = new ObservableCollection<Student>(search.students());
listBoxSS.ItemsSource = students;
students.Clear();
students.Add(new Student("ABC"));
And you should remove the ItemsSource="{Binding Items}"
declaration from your XAML.
您应该从XAML中删除ItemsSource="{绑定项}"声明。
#5
1
Assign the ItemsSource property of the listbox to a public property inside the form's class. Then try adding a removing from that, calling PropertyChanged inside the setter, instead of calling clear directly on the listbox items source.
将列表框的ItemsSource属性分配给表单类中的公共属性。然后尝试添加一个删除,在setter中调用PropertyChanged,而不是直接在listbox项源上调用clear。
#6
1
Late to the party I know but I think this answer isn't quite clear above. Its related to the rogue characters post, but this also causes the exception:
我知道,但我认为这个答案还不太清楚。它与流氓人物的帖子有关,但这也导致了一个例外:
<ItemsControl ItemsSource="{Binding AnObservableCollection}">
<Label Content="{Binding Name}"/>
</ItemsControl>
When what you meant was:
你的意思是:
<ItemsControl ItemsSource="{Binding AnObservableCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Its easy as a newcomer (or before first morning coffee) to think the first is correct and the exception in no way explains what is wrong.
作为一个新人(或者在早上喝咖啡之前),认为第一个是正确的,而这个例外是不可能解释什么是错误的。
#7
0
I had this same problem, and I eventually realized that I was trying to add a new item directly to the control's ItemsSource, rather than to the ObservableCollection that served as the ItemsSource.
我遇到了同样的问题,最终我意识到我试图直接向控件的ItemsSource添加一个新项,而不是作为ItemsSource的ObservableCollection。
I figured I'd post this, as it might help other novice wpfers.
我想我应该把这个贴出来,因为它可能会帮助其他新手。