【发布时间】: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