ListView动态资源

发布于 2024-10-16 07:07:16 字数 1266 浏览 3 评论 0原文

当尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-10-23 07:07:16

如果将 ObservableCollection 定义为窗口代码隐藏中的属性,则可以使用数据绑定来设置 ItemsSource,如下所示:

<ListView Grid.Column="1" Grid.Row="8" Grid.RowSpan="4" ItemsSource="{Binding Path=tasksResponsibilitiesCollection, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <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>

请注意 tasksResponsibilitiesCollection 必须是窗口代码隐藏中的公共属性。

If you ObservableCollection is defined as a property in the code-behind of the window then you could use data binding to set ItemsSource, like this:

<ListView Grid.Column="1" Grid.Row="8" Grid.RowSpan="4" ItemsSource="{Binding Path=tasksResponsibilitiesCollection, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <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>

Please note that tasksResponsibilitiesCollection must be a public property in you window code-behind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文