如果我说对了,那么您想要的是具有适当 CommandParameters 的命令。
public class Car
{
public Car(ParkingLot lot)
{
_parkingLot = lot;
}
public string Color { get; set; }
public ParkingLot ParkingLot
{
get
{
return _parkingLot;
}
}
private ParkingLot _parkingLot;
}
public class ParkingLot : ObservableCollection<Car>
{
public Car SelectedCar { get; set; }
public ICommand ShowNextCarCommand {
get
{
if (_showNextCar == null)
{
_showNextCar = new DelegateCommand(OnShowNextCar);
}
return _showNextCar;
}
}
private void OnShowNextCar()
{
string currentColor = SelectedCar.Color;
//Write proper logic to get the next Car. Here you got the currently selected car with you and the color
SelectedCar = this.NEXT(s => s.Color == currentColor); //Write the NEXT() logic
}
ICommand _showNextCar;
}
现在只需设置 Button.Command="{Binding ParkingLot.ShowNextCarCommand}" 即可控制 ParkingLot 视图模型类并找到 Next 相同颜色的汽车并将其再次设置为 SelectedCar 属性。我假设您将在所有这些属性中都有 RaisepropertyChanged。我使用简单的 Prism 的 DelegateCommand