locked
How to get Listview ItemClick Vallue in Relay Command RRS feed

  • Question

  • 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.

    <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

    =========

    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();
                    }));
            }
    
        }

    Wednesday, May 22, 2013 6:06 AM

Answers

  • WinRT do not support EventToCommand or RelayCommand mechanism out of the box. Try using WinRTBehaviors or MVVM Light to implement the same.

    Thanks,
    Sachin
    My Samples | Personal Website



    • Edited by Sachin S Wednesday, May 22, 2013 6:45 AM
    • Proposed as answer by Jesse Jiang Friday, May 24, 2013 7:10 AM
    • Marked as answer by Jesse Jiang Monday, May 27, 2013 4:22 AM
    Wednesday, May 22, 2013 6:42 AM
  • Any kind of WPF/Silverlight Triggers are not part of Win RT XAML implementation. I have used Win8nl for this EventToCommand in combination of MVVM Light.

    These are now very easy to use from NuGet package source, just right click project and select Manage NuGet packages. In popup window you  can search online for Win8nl MVVM Light and install them.

    For Selected Item thing, I think you can even have a Selected Item property in View Model and in its setter you can do what ever you need (you may want to even validate if it was changed before doing something).

    If you only want to go Command way then you may still use the property in command action. 


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!

    • Proposed as answer by Jesse Jiang Friday, May 24, 2013 7:10 AM
    • Marked as answer by Jesse Jiang Monday, May 27, 2013 4:22 AM
    Wednesday, May 22, 2013 6:56 AM

All replies

  • WinRT do not support EventToCommand or RelayCommand mechanism out of the box. Try using WinRTBehaviors or MVVM Light to implement the same.

    Thanks,
    Sachin
    My Samples | Personal Website



    • Edited by Sachin S Wednesday, May 22, 2013 6:45 AM
    • Proposed as answer by Jesse Jiang Friday, May 24, 2013 7:10 AM
    • Marked as answer by Jesse Jiang Monday, May 27, 2013 4:22 AM
    Wednesday, May 22, 2013 6:42 AM
  • Any kind of WPF/Silverlight Triggers are not part of Win RT XAML implementation. I have used Win8nl for this EventToCommand in combination of MVVM Light.

    These are now very easy to use from NuGet package source, just right click project and select Manage NuGet packages. In popup window you  can search online for Win8nl MVVM Light and install them.

    For Selected Item thing, I think you can even have a Selected Item property in View Model and in its setter you can do what ever you need (you may want to even validate if it was changed before doing something).

    If you only want to go Command way then you may still use the property in command action. 


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!

    • Proposed as answer by Jesse Jiang Friday, May 24, 2013 7:10 AM
    • Marked as answer by Jesse Jiang Monday, May 27, 2013 4:22 AM
    Wednesday, May 22, 2013 6:56 AM