Reputation: 1026
I am making a library management software. When the user clicks on a book, I want to display the full information about the book. One of the properties of the type Book is Tags. Because a book may have many tags, I decided to use a list view to display the tags.
I want to select the tags in the list view. How do I do it? I found this question. The accepted answer says to use a method ListView.Select()
which unfortunately does not exist. Also, ListView.Items[0].Selected = true;
does not compile. This is the error:
Error CS1061 'object' does not contain a definition for 'Selected' and no accessible extension method 'Selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Edit: Someone asked for code. Here it is.
This is the listview:
<ListView x:Name="TagsListView"
SelectionMode="Multiple"
ItemsSource="{x:Bind Tags}"
Grid.Row="4"
Grid.Column="1"/>
this is the code behind:
public sealed partial class BookInfo_View : Page
{
//don't worry about DataAccess.
private Book book = new Book();
private List<string> Tags = DataAccess.GetTags();
public BookInfo_View()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
book = (Book)e.Parameter;
//dont worry about how this works. This line of code gives me the tags
string[] selectedTags = book.Tags.Split(';', System.StringSplitOptions.RemoveEmptyEntries);
//here i want to select the selected tags
}
}
This is the book class:
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public string ISBN { get; set; }
public int Quantity { get; set; }
public string CoverImageLocation { get; set; }
public string Tags { get; set; }
}
Edit: I feel you people did not understand the question. The problem is that
ListView.Items[0].Selected = true;
The above line of code does not compile! It gives the error mentioned above
Upvotes: 1
Views: 511
Reputation: 1595
As you are updating ListView
selected items in OnNavigatedTo
, your ListView might not be initialized with the data at that time, instead, try updating your ListView in page Loaded
event as below.
public sealed partial class BookInfo_View : Page
{
//don't worry about DataAccess.
private Book book = new Book();
private List<string> Tags = DataAccess.GetTags();
private string[] selectedTags;
public BookInfo_View()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
book = (Book)e.Parameter;
//dont worry about how this works. This line of code gives me the tags
selectedTags = book.Tags.Split(';', System.StringSplitOptions.RemoveEmptyEntries);
//here i want to select the selected tags
this.Loaded += OnPageLoaded;
}
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
foreach (string selectedTag in selectedTags)
{
TagsListView.SelectedItems.Add(selectedTag);
}
}
}
Upvotes: 1
Reputation: 1976
base on this, I think you should add your tags to TagsListView.SelectedItems
Upvotes: 0
Reputation: 34180
ListView.Select()
just activates the control (sets focus to it) and does not select its items, so it is not what you are looking for.
But this one that you already mentioned is the answer for your question:
ListView1.Items[0].Selected = true;
however you should use the Name propery of exsiting ListView instance and not ListView
itself. it should be something like listView1
if you have not renamed it.
Upvotes: 0