1

I have looked all over for an answer to this with no luck. I'm using MVVM Light and I have a ListView in one of my views. I would like to find a way to fire a command when the selected item in my ListView is clicked using the 'Event to Command' built into MVVM Light. At this point, I have no clue how to go about doing this. Any help you could give me would be greatly appreciated.

From my GameView.xaml

<ListView ItemsSource="{Binding Adventurers}"
              Name="AdvListView"
              ScrollViewer.CanContentScroll="False"
              Background="Gray"
              BorderBrush="Transparent"
              Grid.Column="1"
              Grid.ColumnSpan="3"
              Grid.Row="2">

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cmd:EventToCommand Command="{Binding ShowAdvCommand}"
                                    CommandParameter="{Binding SelectedItem,
                                          ElementName=AdvListView}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

        <ListView.View>
            <GridView>
                <GridViewColumn Width="Auto" Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Width="Auto" Header="Level" DisplayMemberBinding="{Binding Level}"/>
            </GridView>
        </ListView.View>
    </ListView>

I'm not entirely sure where in the ListView the EventToCommand should go. Also, I understand that this code is completely incorrect.

From GameViewModel.cs

public ICommand ShowAdvCommand { get; private set; }
ShowAdvCommand = new RelayCommand(() => ExecuteShowAdvCommand(), () => true);

private void ExecuteShowAdvCommand()
{
    System.Windows.MessageBox.Show("Firing");
}
Jason D
  • 2,634
  • 6
  • 33
  • 67
  • try [this](http://stackoverflow.com/questions/5868589/mvvm-light-adding-eventtocommand-in-xaml-without-blend-easiery-way-or-snippet) – Rafal Jun 04 '13 at 05:51
  • 1
    Perhaps subscribing to the SelectionChanged instead of the Clicked event would work. Please explain what is supposed to wrong about the command code. – Emond Jun 04 '13 at 05:57
  • @ErnodeWeerd That worked. If you'd like to propose that as an answer I would gladly accept it. – Jason D Jun 04 '13 at 06:32

1 Answers1

2

In the XAML you are subscribing to the Clicked event. Change to the SelectionChanged event to make sure there is a SelectedItem.

A click on the ListView will not always select an item.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • One last thing, how might I access that selected item in code (the associated viewmodel to be specific)? – Jason D Jun 04 '13 at 06:40
  • The Command will part of the ViewModel, so `this` will refer to the ViewModel. If you are interested in the SelectedItem, you are passing it as the parameter but you could also add a dependency property to the ViewModel and bind that to the SelectedItem property of the ListView. If you do that you can access the selected item by accessing the dependency property. – Emond Jun 04 '13 at 06:45
  • Everything seems to be in line now. Thanks again for your help. – Jason D Jun 04 '13 at 07:13
  • Good! No problem, that's what we're here for :) – Emond Jun 04 '13 at 07:24