in my app there is gridview of my custom class. I am using custom data template and values are bound from SQLite. Now when user launch the app, the certain items (NOT SINGLE) should be pre-selected in gridview/listview. Gridview/listview allows multiple selection. How can I achieve this with SelectedItem property ?
UPDATE : I have followed this, it doesn't work for me. Returns 0 selections.
UPDATE 2 : I have 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;
}
You can use SelectedItems property.
I deleted my original answer since yo are not using data binding and my answer wont be useful to you.
I found this just now that might be useful to you:
"SelectedItems property is read only, and cannot be set directly"
Hence, for a solution that may provide help, refer this article.
You can use the SelectedItems property and call SelectedItems.Add() or SelectedItems.Remove() to add/remove items from selection.
If you use ItemsSource binding on the GridView you can use the ListViewExtensions.BindableSelection attached property from the WinRT XAML Toolkit (it should work with a GridView too since it is a subclass of ListViewBase) as in the sample page.
Finally got answer from MSDN. Thanks ForInfo
XAML page
C#