WPF Treeview(带子项)上下移动项目
我在 WPF TreeView 中移动 TreeItem 时遇到问题。 这是我的问题的项目
public class Workflow {
public string Name { get; set; }
public int Rank { get; set; }
private ObservableCollection<Workflow> childWorkflowsValue = new ObservableCollection<Workflow>();
public ObservableCollection<Workflow> ChildWorkflows {
get {
return childWorkflowsValue;
}
set {
childWorkflowsValue = value;
}
}
public Workflow() { }
public Workflow(string name, int rank) {
Name = name;
Rank = rank;
}
}
和移动的逻辑
private void FillWithData() {
myWorkflow.Add(new Workflow("Airbag ausgelöst", 0));
myWorkflow.Add(new Workflow("Abstand halten", 1));
Workflow SubWorkflow = new Workflow("Vorgehen", 2);
SubWorkflow.ChildWorkflows.Add(
new Workflow("Innenraum kontrollieren", 0));
SubWorkflow.ChildWorkflows.Add(
new Workflow("Spuren des Airbags suchen", 1));
SubWorkflow.ChildWorkflows.Add(
new Workflow("Airbag zur seite drücken", 2));
myWorkflow.Add(SubWorkflow);
myTreeView.DataContext = myWorkflow;
}
private void btnMoveUp_Click(object sender, RoutedEventArgs e) {
var currentWf = this.myTreeView.SelectedItem as Workflow;
if (currentWf != null) {
FindRecursive(myWorkflow, currentWf.Name);
}
}
private void FindRecursive(ObservableCollection<Workflow> wf, string searchText) {
foreach (var item in wf) {
if (item.Name == searchText) {
wf.Move(item.Rank, item.Rank-1);
//problem in here
}
FindRecursive(item.ChildWorkflows, searchText);
}
}
当我尝试移动项目时,编译器说:不允许修改集合。 我也在网上搜索了解决方案,但没有机会。 可能有更好的方法来做到这一点。
i have a problem with the movement of a TreeItem in a WPF TreeView.
Here is the Item of my Problem
public class Workflow {
public string Name { get; set; }
public int Rank { get; set; }
private ObservableCollection<Workflow> childWorkflowsValue = new ObservableCollection<Workflow>();
public ObservableCollection<Workflow> ChildWorkflows {
get {
return childWorkflowsValue;
}
set {
childWorkflowsValue = value;
}
}
public Workflow() { }
public Workflow(string name, int rank) {
Name = name;
Rank = rank;
}
}
and the logic of the Movement
private void FillWithData() {
myWorkflow.Add(new Workflow("Airbag ausgelöst", 0));
myWorkflow.Add(new Workflow("Abstand halten", 1));
Workflow SubWorkflow = new Workflow("Vorgehen", 2);
SubWorkflow.ChildWorkflows.Add(
new Workflow("Innenraum kontrollieren", 0));
SubWorkflow.ChildWorkflows.Add(
new Workflow("Spuren des Airbags suchen", 1));
SubWorkflow.ChildWorkflows.Add(
new Workflow("Airbag zur seite drücken", 2));
myWorkflow.Add(SubWorkflow);
myTreeView.DataContext = myWorkflow;
}
private void btnMoveUp_Click(object sender, RoutedEventArgs e) {
var currentWf = this.myTreeView.SelectedItem as Workflow;
if (currentWf != null) {
FindRecursive(myWorkflow, currentWf.Name);
}
}
private void FindRecursive(ObservableCollection<Workflow> wf, string searchText) {
foreach (var item in wf) {
if (item.Name == searchText) {
wf.Move(item.Rank, item.Rank-1);
//problem in here
}
FindRecursive(item.ChildWorkflows, searchText);
}
}
When i try to move the items the compiler says: it is not allowed to modify the collection.
I searched also the web for a solution but no chance.
May be there is a better way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
foreach
迭代集合时,您无法修改集合。我通常使用两种不同的方法来避免此类问题:
或者
使用这种“技巧”,您将能够修改列表:)
You cannot modify a collection while you are iterating through it using a
foreach
.I usually avoid this kind of problem using two different ways:
Or
Using this kind of "trick" you'll be able to modify the list :)