![](/img/trans.png)
[英]Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead when execution twice
[英]Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
我最近是 Binding 和 WPF 的新手,我学会了如何使用listBox
技术创建具有多列的列表框
<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;
但问题是当我尝试使用添加或删除项目或清除
listBoxSS.Items.Clear();
请我需要一个使用项目源的示例,或者我可以添加或删除项目或清除列表的方式。
编辑:
<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()
获取我的数据库中所有员工的列表
listBoxPE.ItemsSource = employees;
Gemployees = employees;
现在我可以在 Gemployees 上执行所有方法
Gemployees.Remove((Student)listBoxSS.SelectedItem);
Gemployees.Add((Student)listBoxSS.SelectedItem);
每当我从 Gemployees 添加或删除项目时, ListView
都会执行刷新! 很酷,但在装订方面仍然有些努力。 现在我正在为每个 ListView 做一个接口类,这样我就可以把我的东西放进去。 它不会在添加项目中执行任何灵活性。
您将ItemsSource
绑定到DataContext
中名为Items
的属性,因此要更新集合,您需要转到DataContext
中的Items
属性并清除它。
此外,如果您希望它在底层集合更改时更新 UI,则Items
属性必须是ObservableCollection
类型,而不是List
类型。
您在后面的代码中设置ItemsSource
的代码是不需要的,应该删除。 您只需将ItemsSource
设置在一个地方,而不是两者都设置。
这是它如何工作的一个简单示例:
// 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<Student>
,当您想清除列表时,您可以调用:
Students.Clear()
奇怪但真实:我的 XAML 文件中的以下错误击键产生了错误“在使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。”:
</ItemsControl.ItemTemplate>x`
注意结束元素标记后的x`
字符。
我知道这个问题大约在 2 年前就已经得到了回答,但是我自己也遇到了这个问题,并且自己想到了一个可能的解决方案,这很有效。 也许这在某些情况下不起作用,也许我根本没有看到什么,但它对我有用,所以我在这里分享它:
listView.ClearValue(ItemsControl.ItemsSourceProperty);
listView.ItemsSource = NewSource;
我真诚地希望这对某人有所帮助。
我认为上面的答案不是很清楚。 与流氓角色有关,这也会导致异常:
<ItemsControl ItemsSource="{Binding AnObservableCollection}">
<Label Content="{Binding Name}"/>
</ItemsControl>
当你的意思是:
<ItemsControl ItemsSource="{Binding AnObservableCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
很容易认为第一个是正确的,而例外决不能解释什么是错的。
您需要处理与ItemsSource
数据绑定的集合。 为了获得集合更改通知(当添加或删除项目时),您应该使用ObservableCollection<T>
:
var students = new ObservableCollection<Student>(search.students());
listBoxSS.ItemsSource = students;
students.Clear();
students.Add(new Student("ABC"));
您应该从 XAML 中删除ItemsSource="{Binding Items}"
声明。
由于语法错误,我出现了相同的错误消息。 事实证明,我不小心在定义了ItemsSource
的TreeView
中定义了一个项目。 我为树视图定义了一个DataTemplate
,但不小心把它直接放在<TreeView>
而不是<TreeView.Resources>
中,导致错误。
我有什么:
<TreeView ItemSource ="{Binding Items}">
<HierarchicalDataTemplate> ... </HierarchicalDataTemplate>
<DataTemplate> ... </DataTemplate>
</TreeView>
我应该有的:
<TreeView ItemSource ="{Binding Items}">
<TreeView.Resources>
<HierarchicalDataTemplate> ... </HierarchicalDataTemplate>
<DataTemplate> ... </DataTemplate>
</TreeView.Resources>
</TreeView>
将列表框的 ItemsSource 属性分配给窗体类中的公共属性。 然后尝试从中添加删除,在 setter 中调用 PropertyChanged,而不是直接在列表框项目源上调用 clear。
始终将 ItemsSource 绑定到 ObservableCollection 以避免内存泄漏。 ObservableCollection 还将导致显示通过添加/删除自动更新。 在我学会这一点之前,我曾经总是绑定到数组。
一个有用的来源: https ://onewindowsdev.com/2016/09/22/a-memory-leak-may-occur-when-you-use-data-binding-in-windows-presentation-foundation/
1) 对于一般的 DataBinding,您绑定的类应该实现 INotifyPropertyChanged。 例外情况是使用 Mode=OneTime 绑定。
2) ItemsSource 应始终是实现 INotifyCollectionChange 的类。 最简单的方法是使用 ObservableCollection。 ObservableCollection 确实有一个构造函数,它接受一个我发现有用的 IEnumerable。
3) 如果 ItemsControl 是 ObservableCollection<SomeClass>,那么 SomeClass 应该实现 INotifyPropertyChange。 然后,您可以安全地绑定到 SomeClass 的各个属性。
我遇到了同样的问题,我最终意识到我试图将新项目直接添加到控件的 ItemsSource,而不是添加到用作 ItemsSource 的 ObservableCollection。
我想我会发布这个,因为它可能会帮助其他新手 wpfers。
对我来说,这是完全不相关的问题。 这是一个愚蠢的语法错误,我忘记用<DataGrid.Style> </DataGrid.Style>
包装我的<style> </style>
> 。
感谢 Microsoft 提供完全令人困惑的错误消息。
对我来说,问题是我误解了控件的说明,不小心把它放在了 datagrid 控件内部而不是外部。
<DataGrid>
<mytag the tag for the open source control />
</DataGrid>
上面有人提到了无关字符,我突然意识到 DataGrid 可能不喜欢那里。 我移动了它,瞧,错误
使用 ItemsSource 时操作无效。 改为使用 ItemsControl.ItemsSource 访问和修改元素。
失去了! 它确实与 ItemsSource 没有任何关系,但是无论解析器生成 Microsoft 错误,它都会让人感到困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.