3

I am not succeeding in sending CommandParameter from ListView item. My code is below.

<ListView x:Name="myList" ItemsSource="{Binding MyData}"                        
     <ListView.View>
          <GridView>
               <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                          <DataTemplate>
                                <Button Command="{Binding Path=DataContext.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" CommandParameter="{Binding SelectedItem, ElementName=myList}" >
                                      <Button.Content>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Path=SomeValue}" />
                                            </StackPanel>      
                                      </Button.Content>
                                 </Button>
                           </DataTemplate>   
                      </GridViewColumn.CellTemplate>
               </GridViewColumn>
          </GridView>
     </ListView.View>
</ListView>

When the item on ListView is clicked, the command is called okay, but the CommandParameter shows Nothing. What's the problem here?

ViewModel command is here:

Public ReadOnly Property MyData As List(Of myObject)
    Get
        Return _myObjectrepo.GetAll()
    End Get 
End Property

Public Property MyCommand As ICommand
    Get
        If _myCommand Is Nothing Then
            _myCommand = New RelayCommandWithParameter(Of myObject)(AddressOf Navigate)
        End If
        Return _myCommand 
    End Get
    Set(value As ICommand)
        _myCommand = value
    End Set
End Property
Private _myCommand As ICommand

...and the procedure where I try to use the CommandParameter

Private Sub Navigate(m As myObject)
    If m IsNot Nothing Then

    End If
End Sub

...but the m is Nothing in the above procedure.

9
  • 1
    {Binding Path=SelectedItem, ElementName=myList} should do the trick Commented Oct 14, 2013 at 9:55
  • Can you post the command code? Could be that the casting is problematic Commented Oct 14, 2013 at 9:55
  • Assuming MyData is a collection of X, make sure the command definition in your viewModel gets X as parameter Commented Oct 14, 2013 at 9:58
  • Added ViewModel code the my initial post... Commented Oct 14, 2013 at 10:33
  • 2
    Try CommandParameter="{Binding}" for the clicked on item Commented Oct 14, 2013 at 10:40

1 Answer 1

9

Copied the answer from comments:

CommandParameter="{Binding}" 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.