CommandParameter is Nothing from ListView command binding

自古美人都是妖i 提交于 2019-12-20 02:04:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!