【问题标题】:ListView DynamicResourceListView 动态资源
【发布时间】:2011-06-23 10:07:06
【问题描述】:

当尝试将 DynamicResource 用作 ListView 的 ItemsSource 时,我的应用程序正在输出错误:

窗口必须是树的根。 无法将 Window 添加为 视觉。

如果我要删除 ItemsSource 并保留代码,那么我不会收到错误消息,并且会显示一个空的 ListView。

我的 Window.XAML 中的 ListView 如下:

<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 中的某个地方定义资源吗?

如果有什么我没有提到的,请告诉我。

感谢您的帮助,

马特

【问题讨论】:

    标签: wpf vb.net .net-3.5 dynamicresource


    【解决方案1】:

    如果您将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 必须是您窗口代码隐藏中的公共属性。

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      相关资源
      最近更新 更多