How to get ListView ItemClick Vallue in Relay Team

Hi, I am working in a Windows store application with the MVVM pattern, and I have some problem in catching the value of itemclick listview in the relay command. Now I got the selected item.But value I don’t know how to get itemclickValue. Here I am attaching my code.

Xaml

<ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemList}" Padding="130,0,0,0" SelectedItem="{Binding SelectedItem,Mode=TwoWay}"> <Triggers:Interactions.Triggers> <Triggers:EventTrigger EventName="SelectionChanged"> <Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem,Mode=TwoWay}"/> </Triggers:EventTrigger> </Triggers:Interactions.Triggers> </ListView> 

ViewModel Code

 private Item _selectedItem; public Item SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged("SelectedTrends"); } } private RelayCommand<Item> _selectedItemCommand; public RelayCommand<Item> SelectedItemCommand { get { return this._selectedItemCommand ?? (this._selectedItemCommand= new RelayCommand<Item>(item=> { MessageDialog messagedialog = new MessageDialog(item.Name,"Test"); messagedialog.ShowAsync(); })); } } 
+6
source share
2 answers

Here's a bit of redundancy:

Option 1: Store CommandParameter:

 private Item _selectedItem; public Item SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged("SelectedTrends"); } } private RelayCommand _selectedItemCommand; public RelayCommand SelectedItemCommand { get { return this._selectedItemCommand ?? (this._selectedItemCommand= new RelayCommand(() => { MessageDialog messagedialog = new MessageDialog(selectedItem.Name,"Test"); messagedialog.ShowAsync(); })); } } 

and xaml:

 <ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemList}" SelectedItem="{Binding SelectedItem,Mode=TwoWay}" Padding="130,0,0,0"> <Triggers:Interactions.Triggers> <Triggers:EventTrigger EventName="SelectionChanged"> <Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" /> </Triggers:EventTrigger> </Triggers:Interactions.Triggers> </ListView> 

Option 2: Replace the SelectedItem binding:

 <ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemList}" Padding="130,0,0,0"> <Triggers:Interactions.Triggers> <Triggers:EventTrigger EventName="SelectionChanged"> <Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstItem}"/> </Triggers:EventTrigger> </Triggers:Interactions.Triggers> </ListView> 

and remove the SelectedItem property from ViewModel if you don't need it for something else.

EDIT

If you want to handle the click event on an element, you need to move the behavior to the parent ItemTemplate DataTemplate, such as the grid in which the controls are placed. This allows you to handle the click event on an element.

+7
source

To solve the problem, I rated the setter attributed if there is a Null reference. Then it worked perfectly, and the event was not selected anymore, select other elements.

 <ListView Name="lstView" ItemsSource="{Binding Path=SimResults}" > <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstView}" /> </i:EventTrigger> </i:Interaction.Triggers> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="FileUniqueID" Width="Auto" DisplayMemberBinding="{Binding Path=FileUniqueID}" /> <GridViewColumn Header="XML" Width="Auto" DisplayMemberBinding="{Binding Path=XML}" /> <GridViewColumn Header="Request" Width="Auto" HeaderStringFormat="" DisplayMemberBinding="{Binding Path=RequestShort}" /> <GridViewColumn Header="RequestDate" Width="Auto" DisplayMemberBinding="{Binding Path=RequestDate}" /> <GridViewColumn Header="Response" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseShort}" /> <GridViewColumn Header="ResponseDate" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseDate}" /> <GridViewColumn Header="ResendCounter" Width="Auto" DisplayMemberBinding="{Binding Path=ResendCounter}" /> </GridView.Columns> </GridView> </ListView.View> </ListView> 
0
source

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


All Articles