Trigger interaction before selectionChanged of ListPicker in Windows Phone 8

I have a problem when a trigger appears in the ViewModel. The selected item (parameter) has a previously selected item. I need the just selected item as a parameter for selectionChanged.

I am new to WP8. Below is the code

<toolkit:ListPicker Header="Background" ExpansionMode="FullscreenOnly" Template="{StaticResource ListPickerControlTemplate}" VerticalAlignment="Top" ItemsSource="{Binding Path=Buildings.ObjectList}" Margin="0" x:Name="buldings" Padding="0"> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding Path=BuildingSelectionCommand}" CommandParameter="{Binding Path=SelectedItem, ElementName=buldings}" /> </i:EventTrigger> </i:Interaction.Triggers> 

Thanks Vinod

+6
source share
3 answers

Usually you should get the current SelectionItem passed as a parameter to your command. Since the event is recorded in the past tense, so you should get the current SelectedItem, not the previous one.

What you can try is to add the binding for SelectedItem to your ListPicker and omit passing SelectedItem to your command as a parameter.

 <toolkit:ListPicker SelectedItem="{Binding SelectedBuilding, Mode=TwoWay}" > <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding Path=BuildingSelectionCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> </toolkit:ListPicker> 

Then, your team must access the SelectedBuilding property to execute

  public class BuildingSelectionCommand{ // a reference to your ViewModel that contains the SelectedBuilding-Property public BuildingsViewModel ViewModel { get; set; } public bool CanExecute(object parameter) { return ViewModel.SelectedBuilding != null; } public void Execute(object parameter){ var selectedItem = ViewModel.SelectedBuilding; // execute command logic with selectedItem } } 

The code may be different on your side, because it depends on how you implemented ViewModel and Command, but I think you should get it.

Another way without using EventTrigger is to execute the command directly in your SelectedBuilding-Property.

 public Building SelectBuilding{ get { return _selectedBuilding } set{ _selectedBuilding = value; RaisePropertyChanged("SelectedBuilding"); if (BuildingSelectionCommand.CanExecute(_selectedBuilding)) { BuildingSelectionCommand.Execute(_selectedBuilding); } } 
+2
source

XAML:

 <i:EventTrigger EventName="SelectionChanged"> <command:EventToCommand Command="{Binding BuildingSelectionCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> 

Command:

 RelayCommand<SelectionChangedEventArgs> BuildingSelectionCommand { get; set; } BuildingSelectionCommand = new RelayCommand<SelectionChangedEventArgs>(async (args) => { }); 

You just missed the "PassEventArgsToCommand". Make sure your team has SelectionChangedEventArgs.

+2
source

An easy way to solve this problem is to use

 SelectedItem="{Binding SelectedBuilding, Mode=TwoWay}" 

as suggested by Jehof, and get rid of all the startup settings " <i: ", but just process the change in the SelectedBuilding property installer and call the method instead of using commands to transfer the method call. You do not get anything with the command, since here you do not even use CanExecute , but simply add more code.

+1
source

Source: https://habr.com/ru/post/943681/


All Articles