我有一个列表框,在它旁边显示一个项目和一个删除按钮。如何触发将参数传递给视图模型的中继命令,以便执行删除。
示例代码。
<ListBox ItemsSource="{Binding Path=CurrentUserRoles, Mode=TwoWay}" SelectedValuePath="Id" Name="lstRoles" Grid.Row="0" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Button Command="{Binding Path=RemoveFromUserRolesCommand, Mode=TwoWay}" Content="Delete">
</Button>
<TextBlock Text=" "></TextBlock>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
查看型号:
private RelayCommand _removeFromUserRolesCommand = null;
public RelayCommand RemoveFromUserRolesCommand
{
get
{
if (_removeFromUserRolesCommand == null)
{
_removeFromUserRolesCommand = new RelayCommand(
() => this.OnARemoveFromUserRolesCommand(),
() => (this._adminModel != null) );
}
return _removeFromUserRolesCommand;
}
}
private void OnARemoveFromUserRolesCommand()
{
try
{
if (!_adminModel.IsBusy && SelectedAvailableRole != null)
{
...
}
}
catch (Exception ex)
{
...
}
}
但它不起作用。我是 Silverlight 的新手,所以有人遇到过这种情况吗?你能分享吗?