有人知道为什么 CommandParameter 总是为空吗?
TransactionViewModel 类具有要在 ItemsControl 中显示的 TransactionCommands 的集合属性。这些项目的类型是 CommandViewModel。
TransactionBrowserViewModel 具有命令 AddJobForSelectedTransactionCommand。将作为参数传递给 CommandViewModel 的命令。
查看片段:
<ItemsControl Grid.Row="4"
ItemsSource="{Binding TransactionCommands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<telerik:RadButton Content="{Binding DisplayName}"
CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Self}}"
Command="{Binding ViewModel.AddJobForSelectedTransactionCommand, ElementName=userControl}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
UserControl 的代码隐藏:
[Export]
public partial class TransactionBrowserView : UserControl, IView<TransactionBrowserViewModel>
{
[ImportingConstructor]
public TransactionBrowserView()
{
InitializeComponent();
}
[Import]
public TransactionBrowserViewModel ViewModel
{
get { return (TransactionBrowserViewModel)this.DataContext; }
set { this.DataContext = value; }
}
}