CommandParameter与ListView命令绑定无关

时间:2013-10-14 09:53:27

标签: wpf listview commandbinding commandparameter

我没有成功从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上的项目时,该命令被称为okay,但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。

1 个答案:

答案 0 :(得分:8)

从评论中复制了答案:

CommandParameter="{Binding}"