Why SelectedItem from the Listbox I get the previous selected item?

I have a listbox:
<ListBox ItemsSource="{Binding Notes}" 
 HorizontalContentAlignment="Stretch" 
VerticalContentAlignment="Stretch"
Name="NoteList"
 SelectedItem="{Binding SelectedNote}"
>
<ListBox.ItemTemplate>
<DataTemplate>
 <StackPanel Orientation="Horizontal" Background="#FFD7B4F3" >
 <TextBlock Text="{Binding Title}" FontSize="15" Margin="2"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<i:Interaction.Triggers>
 <i:EventTrigger EventName="GotFocus">
 <i:InvokeCommandAction Command="{Binding ActivatePanelCommand}" CommandParameter="{Binding SelectedNote}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
 </ListBox>


ViewModel Code:
class MainVM : INotifyPropertyChanged
{
 public static ApplicationContext DB { get; private set; }
 IEnumerable<Note> notes;
 Note selectedNote;

 public IEnumerable<Note> Notes
{
 get { return notes; }
set
{
 notes = value;
OnPropertyChanged();
}
}

 public Note SelectedNote
{
 get { return selectedNote; }
set
{
 selectedNote = value;
OnPropertyChanged();
}
}

 public RelayCommand ActivatePanelCommand
{
get
{
 return activatePanelCommand ??
 (activatePanelCommand = new RelayCommand((selectedItem) =>
{
 Note note = selectedItem as Note;
 if(note != null)
{
MessageBox.Show(note.Title);
}
}));
}

 public MainVM()
{
 CurrentContentVM = new WriteNoteVM();
 DB = new ApplicationContext();
DB.Notes.Load();
 Notes = DB.Notes.Local.ToBindingList();
}

 public event PropertyChangedEventHandler PropertyChanged;
 public void OnPropertyChanged([CallerMemberName]string prop = "")
{
 if (PropertyChanged != null)
 PropertyChanged(this, new PropertyChangedEventArgs(prop));

}
 }


The problem is that from SelectedNote I get the previous selected item(and at the first click nothing displays, as SelectedNote initially null). What to do?
April 3rd 20 at 18:43
2 answers
April 3rd 20 at 18:45
Solution
I'm no expert, but maybe the GotFocus event is triggered before the value is assigned to the selected element SelectedItem.

To solve you can.

Interactivity in addition to the library connect library interactions:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei ="http://schemas.microsoft.com/expression/2010/interactions"


The connection of events will look like this:
<i:Interaction.Triggers>
 <i:EventTrigger EventName="GotFocus">
 <ei:CallMethodAction MethodName="NoteList_GotFocus"
 TargetObject="{Binding }"/>
</i:EventTrigger>
</i:Interaction.Triggers>


ViewModel:
public void NoteList_GotFocus(object sender, RoutedEventArgs e)
{
 if (e.OriginalSource is ListBoxItem lvi)
{
 Note note = lvi.DataContext as Note;
MessageBox.Show(note.Title);
}
}


Proof:
5dfc71a903242851876417.gif
April 3rd 20 at 18:47
Solution
Maybe the GotFocus fires SelectionChanged faster.

To wait for the correct processing of both events snippet:
Note note = selectedItem as Note;
if(note != null)
{
MessageBox.Show(note.Title);
}

it makes sense to wrap it in Dispatcher.BeginInvoke().

Find more questions by tags WPFC#