[英]Datagrid only works when AutoGenerateColumns = true
I have a datagrid with an itemssource 我有一个带有itemssource的数据网格
ItemsSource="{Binding Path=SelectedCollection, Mode=TwoWay}"
when I set AutogenerateColums = true, everything works. 当我将AutogenerateColums设置为true时,一切正常。 But when I set it to alse add the following line to show just 1 column: 但是当我将其设置为alse时,添加以下行以仅显示1列:
<DataGridTextColumn Width="2*" Binding="{Binding Path=Domain}" Header="Domein"/>
Exception: 例外:
Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception Operation is not valid while ItemsSource is in use. 向类型'System.Windows.Controls.ItemCollection'的集合中添加值引发异常。使用ItemsSource时操作无效。 Access and modify elements with ItemsControl.ItemsSource instead. 而是使用ItemsControl.ItemsSource访问和修改元素。
I'm not trying to add anything to the collection anywhere. 我并不是想在任何地方将任何东西添加到集合中。
On the same page I have another datagrid with a different collection that just works the way I described here above without any problems. 在同一页面上,我还有另一个具有不同集合的数据网格,该数据网格的工作方式与我在此描述的方式相同,没有任何问题。 The only differences I can think of is that the second collection is made up of object in another namespace but it should just exist in the same datacontext. 我能想到的唯一区别是第二个集合是由另一个命名空间中的对象组成的,但它应该只存在于相同的数据上下文中。
Anyone any ideas? 有任何想法吗?
EDIT: In design time I get the following message: Cannot resolve property Domain in Data Context of type .... 编辑:在设计时,我收到以下消息:无法解析类型...的数据上下文中的属性域。
You should add custom column inside Columns tag . 您应该在Columns标签内添加自定义列 。 I suspect you are adding directly under DataGrid tag. 我怀疑您是直接在DataGrid标签下添加。 It should be: 它应该是:
<DataGrid ItemsSource="{Binding Path=SelectedCollection, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn/>
</DataGrid.Columns>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.