Reputation: 71
I am having trouble with the SelectionChanged
event trigger for a combobox on my data grid.
The combobox items source is bound to my view model. The combobox selected item is also bound to my view model.
When I change the combobox selected item, a delegate command on the view model is fired which goes off and updates etc. But when I scroll the data grid the SelectionChanged
event also fires.
I am also using Prism MVVM.
Here is my XAML:
<DataGrid.Columns>
<DataGridTemplateColumn Header="Selected Reason" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Reasons, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding SelectedReason}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=DataContext.ReasonChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Here is my View Model command:
private DelegateCommand<T> _reasonChangedCommand;
public DelegateCommand<T> ReasonChangedCommand =>
_reasonChangedCommand ?? (_reasonChangedCommand = new DelegateCommand<T>(ReasonChanged));
private async void ReasonChanged(T obj)
{
if (obj != null)
{
await _updateRepository.UpdateAsync(obj);
}
}
All articles I have searched discuss the command not firing and not firing when the data grid is scrolled. Any help or direction to an article would be much appreciated.
Many Thanks.
Upvotes: 3
Views: 1300
Reputation: 71
After playing around with different event triggers, using DropDownClosed
event resolves this issue.
Upvotes: 4