Good afternoon, there is a collection which is filled from the MySQL base:

while (contains_result.Read()) { ItemsForContains.Add( new Contains() { CId = contains_result["id"].ToString(), ProfId = contains_result["profId"].ToString() }); } 

Class with properties:

 public class Contains : INotifyPropertyChanged { private string _cid; private string _groupid; public string CId { get { return _cid; } set { if (_cid != value) { _cid = value; RaisePropertyChanged(); } } } public string GroupId { get { return _groupid; } set { if (_groupid != value) { _groupid = value; RaisePropertyChanged(); } } } protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); public event PropertyChangedEventHandler PropertyChanged; } 

The whole thing is tied to a ListView (ItemsSource). I'm trying to do something when I clicked on one of the lines of the ListView for example, I gave out a MessageBox with the data of exactly the line to which I clicked. I do it like this: Binding to the SelectedItem in the ListView:

 SelectedItem="{Binding SelectedItemsContains}" 

SelectedItemsContains:

 private Contains _SelectedItemsContains; public Contains SelectedItemsContains { get { return _SelectedItemsContains; } set { _SelectedItemsContains = value; RaisePropertyChanged(); } } protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); public event PropertyChangedEventHandler PropertyChanged; 

Well, the event itself on click:

 private void editProf_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { MessageBox.Show(SelectedItemsContains.CId); } 

Displays "Object reference does not indicate an object instance." And if I add the line SelectedItemsContains = new Contains () to the editProf_MouseLeftButtonUp method; the error disappears, but displays just an empty MessageBox

  • And if you do this: pastebin.com/45Y6s7vz ? (this is only for debugging, do not do it in real code) - VladD
  • The error is the same as before, and if like this pastebin.com/vsNxr1kd , then an empty message, just like before. - Dmitry
  • If I write SelectedItemsContains = new Contains (); I get this result, assigning empty values, can you somehow pull this line straight from the collection? - Dmitry

1 answer 1

The fact is that the first click only selects the element, but the second click on the same element should produce information. Therefore, you need to check for null , and then output.

 private void editProf_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (SelectedItemsContains != null) MessageBox.Show(SelectedItemsContains.CId); } 
  • Also: (only now if SelectedItemsContains = new Contains (); not to create it does not work - Dmitry
  • Дело в том, что первый клик лишь только выделяет элемент, а вот второй клик по этому же элементу должен выдавать информацию. Who else said this? - Ev_Hyper
  • @ Dmitry This is a completely unnecessary operation: SelectedItemsContains = new Contains(); at you the binding on this variable costs in ListView! - Bulson
  • Surprisingly, it worked, it seems, and did not change anything, just restarted the studio ... - Dmitry
  • @ Dmitry, I looked, I didn’t notice anything like that. Do you have a collection from the database displayed normally? Put a breakpoint on the entry to the editProf_MouseLeftButtonUp method and look at the SelectedItemsContains value, what does it give? - Bulson