I have a listbox that, when an item is selected, invokes a method that executes a Stored Procedure.
The problem is that when the first item is selected, my PropertyChanged event doesn’t fire unless the selection is changed from one item to another. Thus the second item SelectedItem PropertyChanged notification is fired, but it looks like selecting the first item is just seen as entering the listbox, instead of entering the listbox AND selecting the item the click occurs on.
Also, I can’t just click twice on the same item to get the notification to fire, I have to actually select a different property for the event to occur.
What is the best way to get the item I first click on upon entering the listbox to be the SelectedItem, having the PropertySelected/Property Changed event firing on this item? I hope this is clear.
Below is my code, thanks in advance!
In my viewmodel:
public ObjectClass SelectedObject
{
get { return _SelectedObject; }
set
{
_SelectedObject = value;
base.OnPropertyChanged("SelectedObject");
}
}
void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "SelectedObject" : UpdateSelectedStuffList.StoredProcedureMethod(this);
}
}
In my view:
<ListBox ItemsSource="{Binding Path=ObjectCollection, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="objectName"
SelectedItem="{Binding Path=SelectedObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
I was thinking that instead of using PropertyChangedEventArgs, there would be something like “PropertySelectedEventArgs.” OR, maybe I need to implement INotifyPropertyChanging?
If you want this to fire even if you select the same item twice in a row, I would look at OnClick. Otherwise, consider setting the selected index to -1 so that when the first item is selected by the user, it will have a changed value.