3

我没有成功从 ListView 项发送 CommandParameter。我的代码如下。

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

单击 ListView 上的项目时,命令调用正常,但 CommandParameter 显示 Nothing。这里有什么问题?

ViewModel 命令在这里:

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

...以及我尝试使用 CommandParameter 的过程

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

    End If
End Sub

...但是在上述过程中 m 是 Nothing 。

4

1 回答 1

9

复制评论中的答案:

CommandParameter="{Binding}" 
于 2013-11-12T18:17:53.313 回答