i have these lines of codes in my xaml :
<ListView x:Name="ContactsList" MouseDoubleClick="ContactsList_MouseDoubleClick" HorizontalAlignment="Left" Height="453" Margin="10,10,0,0" VerticalAlignment="Top" Width="235" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListView.View> <GridView> <GridViewColumn Header="LastName" Width="110" DisplayMemberBinding="{Binding LastName}" /> <GridViewColumn Header="FirstName" Width="115" DisplayMemberBinding="{Binding FirstName}" /> </GridView> </ListView.View> </ListView>
and i got a table of data name Contacts and this is my addContact function :
ContactDBEntities db = new ContactDBEntities();
Contact newContact = new Contact(); newContact.FirstName = FName.Text; newContact.LastName = LName.Text; newContact.Mobile = Mob.Text; newContact.Telephone = Tel.Text; newContact.Mail = Mail.Text; db.Contacts.Add(newContact); db.SaveChanges();
and my show contacts function is :
IQueryable<Contact> items = from c in db.Contacts where c.LastName.Contains(s) || c.FirstName.Contains(s) select c; ContactsList.ItemsSource = items.ToList();
and all i want is to know which object has clicked by the user in listview so i can access to it's information. shoud i use the below function? and if i should what i have to write in it?
private void ContactsList_MouseDoubleClick(object sender, MouseButtonEventArgs e) { }
Hook the SelectionChanged event to run your function whenever they select a different item. That way your function will work regardless of the input device they use (keyboard arrows, etc).
Then in your function just read listView.SelectedIndices
which contains a collection of all selected indices.
If you just want to respond to double-clicks and a single item, stay with MouseDoubleClick and get your item by: ListViewItem lvi = listView.SelectedItems[0];
In WPF, databinding is treated in a totally different manner, as compared to Winforms.
If you can follow this example , you should be able to accomplish what you need, but you need to modify your code.
In your case, each item in the ListView is a Contact
Object, so you need to bind the SelectedItem
property of ListView to a property say (SelectedContact) in the class that implements INotifyPropertyChanged
.
private Contact _selectedContact;
public Contact SelectedContact { get { return this._selectedContact; }
set { if (value != this._selectedContact) { this._selectedContact= value; NotifyPropertyChanged(); } } }
Word of advice : If you are not using Data binding and Property Changed via INotifyPropertyChanged functionality then you are still living in the old world of WindowsForm world. I would request you to learn it and adapt in your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.