简体   繁体   English

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

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

I have a piece of code doesn't work properly. 我有一段代码无法正常工作。 If I execute the btnNew once there is no problem. 如果我执行btnNew一旦没有问题。 If I execute twice I get a error of... 如果我执行两次,我会收到错误...

Operation is not valid while ItemsSource is in use. 使用ItemsSource时,操作无效。 Access and modify elements with ItemsControl.ItemsSource instead. 使用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 ClassA.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. 如果我在主类中使用if语句它将消除问题然后它将在组合框中创建重复的字符串。 Then I am asking myself do I need to create a method to handle distinct? 然后我问自己我是否需要创建一个处理不同的方法? I am not sure on this. 我不确定。

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. 当我使用try / catch时,它会捕获错误并显示消息。 So this is not good either. 所以这也不好。

So my question is can anyone tell me how to solve this problem? 所以我的问题是谁能告诉我如何解决这个问题?

You cannot set both the ItemsSource property and the Items property together. 您不能同时设置ItemsSource属性 Items属性。 Try simply removing your calls to cbTitle.Items.Clear() which is unnecessary if you are setting the ItemsSource property on the next line anyway. 尝试简单地删除对cbTitle.Items.Clear()调用,如果你在下一行设置ItemsSource属性,这是不必要的。


UPDATE >>> 更新>>>

You only need to set the ItemsSource property once, preferably in XAML: 您只需要设置一次ItemsSource属性,最好是在XAML中:

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

Once this is done, you shouldn't set 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;
}

Thanks Sheridan. 谢谢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. 我没有使用xaml因为它给了我问题。 You may remember I opened a thread about combobox when navigating through records. 您可能还记得我在浏览记录时打开了关于组合框的线程。 I managed to solve that problem by using for loop. 我设法通过使用for循环解决了这个问题。 have a look at my other thread, if you wish, here 看看我的其他线程,如果你愿意的话, 在这里

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和它的反刍操作,所以我会发现它会很有趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ItemsSource 时操作无效。 改为使用 ItemsControl.ItemsSource 访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead 使用ItemsSource时,操作无效。 使用ItemsControl.ItemsSource访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource WPF数据网格:在使用ItemsSource时,操作无效。 - WPF Data Grid : Operation is not valid while ItemsSource is in use. 使用ItemsSource时操作无效。 但是我没有混 - Operation is not valid while ItemsSource is in use. But i'm not mixing 显示ItemsControl.ItemsSource是否为null - Show if ItemsControl.ItemsSource is null ItemsControl.ItemsSource,绑定无效 - ItemsControl.ItemsSource, binding doesn't work 根据另一个 ItemsControl 的选定对象更新 ItemsControl.ItemsSource - Update ItemsControl.ItemsSource based on selected object of a another ItemsControl 如何将ItemsControl.ItemsSource与XAML中的属性绑定? - How can I bind an ItemsControl.ItemsSource with a property in XAML? 访问Itemscontrol的ItemsSource中的对象 - Access objects in Itemscontrol's ItemsSource 无法使用列表框DataTemplate:当ItemsSource正在使用时,操作无效 - Can't use list box DataTemplate: Operation is not valid while ItemsSource is in use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM