【问题标题】:CommandParameter is Nothing from ListView command bindingCommandParameter 不是 ListView 命令绑定中的任何内容
【发布时间】:2013-10-14 09:53:26
【问题描述】:

我没有成功从 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上的item被点击时,命令调用正常,但是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。

【问题讨论】:

  • {Binding Path=SelectedItem, ElementName=myList} 应该可以解决问题
  • 可以发下命令代码吗?可能是选角有问题
  • 假设MyData 是X 的集合,请确保viewModel 中的命令定义将X 作为参数
  • 在我最初的帖子中添加了 ViewModel 代码...
  • 为点击的项目尝试 CommandParameter="{Binding}"

标签: wpf listview commandbinding commandparameter


【解决方案1】:

从cmets复制答案:

CommandParameter="{Binding}" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-04-16
    • 1970-01-01
    • 2018-08-13
    • 2020-02-10
    • 2021-06-10
    • 2020-05-23
    相关资源
    最近更新 更多