wpf 使用 ItemsSource 时操作无效。执行两次时,改为使用 ItemsControl.ItemsSource 访问和修改元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20522422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:11:29  来源:igfitidea点击:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead when execution twice

c#wpfwinforms

提问by bucketblast

I have a piece of code doesn't work properly. If I execute the btnNew once there is no problem. If I execute twice I get a error of...

我有一段代码不能正常工作。如果我执行 btnNew 一次就没有问题。如果我执行两次,我会得到一个错误...

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。

Main class

主班

ClassA obj = new ClassA();         

private void btnNew_Click(object sender, RoutedEventArgs e)
{
    //List strings for clearing and then creating new strings for Title combobox
    ObservableCollection<string> calledList = obj.GetList();
    cbTitle.Items.Clear();
    cbTitle.ItemsSource = calledList;
}

ClassA.cs

类A.cs

private ObservableCollection<string> data = new ObservableCollection<string>();

public ObservableCollection<string> GetList()
{
    return data;
}

public void SimpleNew()
{
    data.Add("A");
    data.Add("B");
}

if I use a if statement in the main class it will eliminate the problem then it will create duplicate strings in the combobox. Then I am asking myself do I need to create a method to handle distinct? I am not sure on this.

如果我在主类中使用 if 语句,它将消除该问题,然后它将在组合框中创建重复的字符串。然后我问自己是否需要创建一种方法来处理不同的?我不确定这一点。

This my if statement in the main class

这是我在主类中的 if 语句

if (cbTitle.Items.Count == 0)
{
    ObservableCollection<string> calledList = obj.GetList();
    cbTitle.Items.Clear();
    cbTitle.ItemsSource = calledList;
}

When I used try/catch it catches error and shows the message. So this is not good either.

当我使用 try/catch 时,它会捕获错误并显示消息。所以这也不好。

So my question is can anyone tell me how to solve this problem?

所以我的问题是谁能告诉我如何解决这个问题?

采纳答案by bucketblast

Thanks Sheridan. I have also discovered that if I do...

谢谢谢里丹。我还发现,如果我...

ObservableCollection<string> calledList = obj.GetList();
calledList.Clear(); // I have to use this line of code
calledList.ItemsSource = calledList;

This solve my problem. I am not using xaml because it gave me problems. You may remember I opened a thread about combobox when navigating through records. I managed to solve that problem by using for loop. have a look at my other thread, if you wish, here

这解决了我的问题。我没有使用 xaml,因为它给我带来了问题。您可能还记得我在浏览记录时打开了一个关于组合框的主题。我设法通过使用 for 循环解决了这个问题。看看我的另一个线程,如果你愿意,在这里

However this is not the final solution. I am learning wpf and its cud operation so it will be interesting what I will discover

然而,这不是最终的解决方案。我正在学习 wpf 及其反刍操作,所以我会发现很有趣

回答by Sheridan

You cannot set both the ItemsSourceproperty andthe Itemsproperty together. Try simply removing your calls to cbTitle.Items.Clear()which is unnecessary if you are setting the ItemsSourceproperty on the next line anyway.

你不能同时设置ItemsSource属性Items属性的放在一起。cbTitle.Items.Clear()如果您ItemsSource无论如何要在下一行设置属性,请尝试简单地删除不必要的调用。



UPDATE >>>

更新 >>>

You only need to set the ItemsSourceproperty once, preferably in XAML:

您只需要设置ItemsSource一次属性,最好是在 XAML 中:

<ComboBox ItemsSource="{Binding Items}" ... />

Once this is done, you shouldn'tset it again. To change the items in the ComboBox, simply change the items in the collection... they are now data bound... this is WPF, not WinForms:

完成此操作后,您不应再次设置它。要更改 中的项目ComboBox,只需更改集合中的项目……它们现在是数据绑定的……这是 WPF,而不是 WinForms:

private void btnNew_Click(object sender, RoutedEventArgs e)
{
    //List strings for clearing and then creating new strings for Title combobox
    ObservableCollection<string> calledList = obj.GetList();
    Items = calledList;
}