How to pre-select multiple list items / gridview in a C # / XAML Windows 8 application?

in my application there is a gridview of my custom class. I use a user data template and the values ​​are bound from SQLite. Now, when the user starts the application, certain elements (NOT SINGLE) must be pre-selected in gridview / listview. Gridview / listview allows multiple selection. How can I achieve this using the SelectedItem property?

UPDATE: I followed this , this does not work for me. Returns 0 choices.

UPDATE 2: I posted the code

void MainPage_Loaded(object sender, RoutedEventArgs e) { using (var db = new SQLite.SQLiteConnection(dbpath)) { lvTags.ItemsSource = db.Table<Database.Tag>(); //lvTags is listview if (MyList.Count > 0) //MyList is the static list of class "Database.Tag" { foreach (var item in MyList) foreach (var lvitem in lvTags.Items) if (lvitem.Equals(item)) lvTags.SelectedItems.Add(lvitem); } } } 

UPDATE 3:

 public override bool Equals(object obj) { Tag tag = obj as Tag; if (this.TagID == tag.TagID && this.TagName == tag.TagName) return true; else return false; } 
+7
source share
4 answers

Finally got a response from MSDN . Thanks ForInfo

XAML Page

 <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <ListView x:Name="listView" SelectionMode="Multiple"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding ID}" Margin="0,0,5,0"/> <TextBox Text="{Binding Title}"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> 

FROM#

 public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); LoadData(); } protected override void OnNavigatedTo(NavigationEventArgs e) { } ObservableCollection<KiwiItem> sourceColl; IList<KiwiItem> selectionList; public void LoadData() { var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite"); // Exec (1) using (var db = new SQLite.SQLiteConnection(dbPath)) { db.DropTable<KiwiItem>(); db.CreateTable<KiwiItem>(); db.RunInTransaction(() => { db.Insert(new KiwiItem() { ID = 1, Title = "MyTitle1" }); db.Insert(new KiwiItem() { ID = 2, Title = "MyTitle2" }); db.Insert(new KiwiItem() { ID = 3, Title = "MyTitle3" }); db.Insert(new KiwiItem() { ID = 4, Title = "MyTitle4" }); }); this.sourceColl = new ObservableCollection<KiwiItem>(); this.selectionList = new List<KiwiItem>(); // Query the db. In practice, fill the sourceColl according to your business scenario foreach (KiwiItem item in db.Table<KiwiItem>()) { this.sourceColl.Add(item); if (item.ID == 2 || item.ID == 4) this.selectionList.Add(item); } } // Exec (2) this.listView.ItemsSource = this.sourceColl; foreach (KiwiItem item in this.selectionList) this.listView.SelectedItems.Add(item); } } public class KiwiItem { [SQLite.AutoIncrement, SQLite.PrimaryKey] public int ID { get; set; } public string Title { get; set; } } 
+6
source

You can use the SelectedItems property.

  // // Summary: // Gets the currently selected items. // // Returns: // A collection of the currently selected items. public IList<object> SelectedItems { get; } 
+1
source

You can use the SelectedItems property and call SelectedItems.Add () or SelectedItems.Remove () to add / remove items from the selection.

If you use the ItemsSource binding in the GridView, you can use the attached ListViewExtensions.BindableSelection property from the WinRT XAML Toolkit (it should work with the GridView too, since it is a subclass of ListViewBase), like on the page.

+1
source

I deleted my original answer, since yo does not use data binding, and my answer will not be useful to you.

I found this now, which may be useful for you:

"The SelectedItems property is read-only and cannot be set directly."

Therefore, for a solution that may help, see this article.

-one
source

All Articles