问题
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.
回答1:
Copied the answer from comments:
CommandParameter="{Binding}"
来源:https://stackoverflow.com/questions/19357611/commandparameter-is-nothing-from-listview-command-binding