【问题标题】:ListView Data Binding for Windows 8.1 Store Apps用于 Windows 8.1 应用商店应用的 ListView 数据绑定
【发布时间】:2013-11-16 00:34:46
【问题描述】:

所以我正在尝试使用Visual Studio 2013 将我制作的桌面程序移动到Windows 8.1 的应用程序中。 Datagrid 在此版本中已被弃用,因此我尝试将它们转换为 ListView。我之前在其他桌面应用程序中使用过ListViews,但看起来发生了很多变化。我遇到的问题是从我的数据库中填充我的ListView。我正在通过WCF Service 连接并单步执行代码,我可以看到我得到了正确的数据,但我无法让它出现在我的 ListView 中。我最喜欢的最终结果是包含 3 列可编辑信息的“列表视图”。以前我会使用ListView.View,然后将GridView 放在那里作为列。但似乎 ListView.View 已被弃用,就像 GridView.Columns 一样。

这是我的ListViewxaml

<ListView x:Name="lvInventory" Grid.Row="2" Style="{StaticResource listViewStyle}" ItemsSource="{Binding}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Height="200"  Width="200">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="tbName" Text="{Binding InventoryName}" Width="200" Foreground="#FF0E0D0D" />
                        <TextBox x:Name="tbQty" Grid.Column="1"/>
                        <TextBox x:Name="tbType" Grid.Column="2"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

在我的代码隐藏中,我正在分配列表视图的 itemssource。

        VMInventory inventory = new VMInventory();            
        inventory.GetList();
        lvInventory.ItemsSource = inventory;

VMInventory 是我的ViewModel,我从WCF service 获取数据,看起来像:

 public async Task GetList()
    {

            this.connection = new InventoryModelEntities(new Uri(url));
            var filteredList = from o in connection.Inventory
                               where o.Have == false
                               select o;
            var query = await Task.Factory.FromAsync((filteredList as DataServiceQuery).BeginExecute(null, null),
                (result) => (filteredList as DataServiceQuery).EndExecute(result)) as IEnumerable<Aurora.InventoryService.Inventory>;

            this.inventoryList = query.ToList();
            this.currentItem = 0;
            this.onPropertyChanged("Current");
            this.IsAtStart = true;
            this.IsAtEnd = (inventoryList.Count == 0);
}

最后一点,我能够将一个文本框添加到Grid,当我将DataBind 它添加到text="{Binding Current.InventoryName}" 时,我能够成功绑定它。

【问题讨论】:

    标签: wpf listview data-binding windows-store-apps windows-8.1


    【解决方案1】:

    如果我理解正确,您的 UI 并没有更新您在 ViewModel 中添加的值,但如果您在 UI 中添加一些值,它会反映在 ViewModel 中。

    如果是这种情况,请使用 ObserableCollection 而不是列表,如果您每次都创建列表,那么您必须在 ViewModel 中实现 INotifyPropertyChanged。

    如果您已经完成了所有这些但仍然没有更新,那么您将在 asy 任务中创建列表,这不是 UI 线程。如果要从非 Ui 线程更新 UI,请使用 Dispatcher 更新 UI。您可以找到很多使用调度程序从非 UI 线程更新 UI 的示例

    【讨论】:

    • UI 根本没有更新,如果我对 UI 进行更改,它不会反映在视图模型中。视图模型填充得很好,只是没有进入 UI。我会尝试将其更改为可观察的集合。回复:异步任务,当我使用“Current.InventoryName”绑定将数据绑定到文本框时,它将数据绑定并显示数据。但是我不能将 current 用于 LV,因为我需要所有这些,而不仅仅是一个。
    • @Kumareshan 是正确的,ObservableCollection 就是答案。如果你想从一个任务(不在 UI 线程上运行)更新它,你需要查看 CoreDispatcher,否则你会收到编组错误。
    • 好的,我将调整我的 var 查询以返回 ObservableCollection 而不是 IEnumerable。一旦我这样做了,那么我的数据绑定将按原样工作吗?当我可以直接访问数据库时,这在 WPF 中要容易得多。
    • @Kumareshan,我最终将 WCF 排除在外,并使用 SQLite,我不知道您可以将其与橱窗商店应用程序一起使用。然后我围绕一个可观察的集合构建了我的视图模型,它工作了!感谢您的帮助!
    猜你喜欢
    • 2016-07-18
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多