How to get Listview ItemClick Vallue in Relay Command
Asked Answered
T

2

6

Hi i'm working in windows store app with MVVM pattern and i have some problem in catch the listview itemclick value in relay command. Now i got the selected item value.But don't know how to get itemclickValue. Here i have attach 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();
                }));
        }

    }
Tasha answered 22/5, 2013 at 5:50 Comment(0)
A
8

There's a bit of redundancy here:

Option 1: Spare the 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 the 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: Spare 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 the ViewModel, unless you need it for something else.

EDIT

If you want to handle the click event on an item, you need to move the behavior to the ItemTemplate DataTemplate parent control, for example the grid in which the controls are placed. This lets you handle the click event on the item.

Acquainted answered 22/5, 2013 at 6:49 Comment(5)
Thanks for your response.Here you given the solution for SelectionChanged event. But i need ItemClick event.Tasha
@user1583834: Nice,. Please mark the question as answered with the check mark next to the answer and upvote if you like.Acquainted
@Marc: I know it is an old question, but in the case of Option 2, if I select an item in the ListView (using right click) and press left click on another item, the selected item is passed, not the clicked one. Do you know a solution for this? I would like to get the ClikedItem regardless of the selection in the ListViewBassist
Hi rhcpfan, I've recently answered a question very similar to yours. Maybe this helps in your case? #29811429 Let me know if it doesn't....Acquainted
Hi I tested the code above and it worked fine. But I have problem. I have different tabs, when I click a different tab with the left click it always runs the SelectionChanged event and I get a Null reference exception. I added the code below.Mattoid
M
0

To resolve the problem I evaluated the setter attributed if there is Null reference. Then it worked fine and the event wasn't thrown anymore choose 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>
Mattoid answered 23/7, 2015 at 8:12 Comment(2)
Hi kleopatra, thank you for your reply and sorry for the late response. I didn't saw the whole XAML structure above, this is why I posted mine as an answer. I thought maybe my approach is wrong because you can also define over a cell template text blockes and add then events. So thought maybe he build his cells with this approach which is different to my. Then he didn't used the list view in an tab view this is why I get the Null exception after switching the tab. This wasn't described. I already solved the problem. The questions wasn't really duplicated. Maybe you can delete the downvote.Mattoid
ahh .. I see - thanks for the clarification :-) Please consider editing your answer again and clarify that it's indeed a solution.Bender

© 2022 - 2024 — McMap. All rights reserved.