ListView动态资源
当尝试使用 DynamicResource 作为 ListView 的 ItemsSource 时,我的应用程序输出错误:
窗口必须是树的根。 无法将窗口添加为子窗口 视觉效果。
如果我要删除 ItemsSource,并保留代码,那么我不会收到错误,并且会显示一个空的 ListView。
我的 ListView 位于 Window.XAML 中,如下所示:
<ListView Grid.Column="1" Grid.Row="8" Grid.RowSpan="4" ItemsSource="{DynamicResource tasksResponsibilitiesCollection}">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn DisplayMemberBinding="{Binding tasksResponsibilitiesName}" Header="Tasks/Responsibility" Width="150" />
<GridViewColumn Header="Member Responsible" Width="120" />
<GridViewColumn Header="Qualifications" Width="110" />
</GridView>
</ListView.View>
</ListView>
ItemsSource 是在代码隐藏中定义的 ObservableCollection。我一直在使用 MSDN 示例 (http://msdn.microsoft.com/en-us/library/ms747048.aspx) 作为创建 ListView 的指南
如何将 ObservableCollection 竞价到 ListView?我是否必须在 Window.XAML 中的某处定义资源?
如果有任何我没有涵盖的内容,请告诉我。
我感谢你的帮助,
马特
When attempting to use a DynamicResource as an ItemsSource for a ListView my application is outputting the error:
Window must be the root of the tree.
Cannot add Window as a child of
Visual.
If I was to remove the ItemsSource, and leave the code alone, then I dont get the error and an empty ListView will display.
My ListView located in my Window.XAML is as follows:
<ListView Grid.Column="1" Grid.Row="8" Grid.RowSpan="4" ItemsSource="{DynamicResource tasksResponsibilitiesCollection}">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn DisplayMemberBinding="{Binding tasksResponsibilitiesName}" Header="Tasks/Responsibility" Width="150" />
<GridViewColumn Header="Member Responsible" Width="120" />
<GridViewColumn Header="Qualifications" Width="110" />
</GridView>
</ListView.View>
</ListView>
The ItemsSource is an ObservableCollection defined in the code-behind. I have been using the MSDN example (http://msdn.microsoft.com/en-us/library/ms747048.aspx) as my guide for the creation of the ListView
How do I go about bidning the ObservableCollection to the ListView? Do I have to define the resource within Window.XAML somewhere?
If there is anything that I haven't covered please let me know.
I appreciate your assistance,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将
ObservableCollection
定义为窗口代码隐藏中的属性,则可以使用数据绑定来设置ItemsSource
,如下所示:请注意
tasksResponsibilitiesCollection
必须是窗口代码隐藏中的公共属性。If you
ObservableCollection
is defined as a property in the code-behind of the window then you could use data binding to setItemsSource
, like this:Please note that
tasksResponsibilitiesCollection
must be a public property in you window code-behind.