wpf 使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。解析 XAML 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15625974/
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 08:18:50  来源:igfitidea点击:

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

wpfxamlobservablecollectionitemssource

提问by Lee Louviere

I get a runtime error that crashes when it tries to parse my XAML.

我收到一个运行时错误,当它尝试解析我的XAML.

<TreeViewItem Header="{Binding Header}" ItemsSource="{Binding DataGroups}">
    <ListView ItemsSource="{Binding DataGroups}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Group Name" DisplayMemberBinding="{Binding Path=Name}" />
                <GridViewColumn Header="Group Type" DisplayMemberBinding="{Binding Path=Type}" />
            </GridView>
        </ListView.View>
    </ListView>
</TreeViewItem>

采纳答案by Lee Louviere

After searching through all the questions related to this, I couldn't find the answer. Most involved people changing the Items property on the TreeViewItem, which will throw this error.

在搜索了与此相关的所有问题后,我找不到答案。大多数涉及人员更改 上的 Items 属性TreeViewItem,这将引发此错误。

In my case, I didn't change anything.

就我而言,我没有改变任何东西。

However, if you notice.

然而,如果你注意到了。

<TreeViewItem Header="{Binding Header}" ItemsSource="{Binding DataGroups}">
    <ListView ItemsSource="{Binding DataGroups}">

I have bound to the same collection twice.

我已经两次绑定到同一个集合。

So, double check you haven't bound to the same collection twice if you get this error.

因此,如果出现此错误,请仔细检查您是否两次绑定到同一个集合。