简体   繁体   English

为什么以前选择的GridView项目不显示为选中状态?

[英]Why don't previously selected GridView items show as selected?

I have a Windows 8 App that uses a GridView to display a collection of Items. 我有一个使用GridView来显示项目集合的Windows 8应用程序。 I've created the app using C# and XAML. 我已经使用C#和XAML创建了该应用程序。 The GridView is multi-selectable. GridView是多选的。

When the user leaves the GridView page I save which items in the grid were selected, to application state, using: navigationHelper_SaveState(). 当用户离开GridView页面时,我使用以下navigationHelper_SaveState().选中网格中的哪些项目保存到应用程序状态: navigationHelper_SaveState().

When the user returns to the GridView page I want to re-select the previously selected items. 当用户返回到GridView页面时,我想重新选择先前选择的项目。

I've confirmed that both saving and loading state work. 我已经确认保存和加载状态均有效。

However, the previously selected items don't get selected when the user returns to the page. 但是,当用户返回页面时,先前选择的项目不会被选择。 From within navigationHelper_LoadState(). navigationHelper_LoadState(). I do this to select all of the previously selected items: 我这样做是为了选择所有先前选择的项目:

for (int i = 0; i < numPrevSelectedItems; i++) {    
  ItemsGridView.SelectedItems.Add(items[i]);
}

Elsewhere on StackOverflow I've read that using SelectedItems.Add() should work. 在StackOverflow的其他地方,我读到使用SelectedItems.Add()应该可以工作。 However, it does not work for me. 但是,它对我不起作用。 No previously selected items are visibly selected when the user returns to the page. 当用户返回页面时,不会明显选择先前选择的项目。

Interestingly enough, ItemsGridView.SelectAll(); 有趣的是, ItemsGridView.SelectAll(); does work. 确实有效。

What's the best way to get this to work properly? 使其正常工作的最佳方法是什么?

You can do something like this: 您可以执行以下操作:

// Get the selected rows
DataGridViewSelectedRowCollection SelectedRowsCollection = ItemsGridView.SelectedRows;

// Clear your ItemsGridView selected rows (Just for testing the following code)
ItemsGridView.ClearSelection();

// Get your previous selected rows again.
foreach (DataGridViewRow item in SelectedRowsCollection)
{
    int idx = ItemsGridView.Rows.IndexOf(item);
    ItemsGridView.Rows[idx].Selected = true;
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM