views:

242

answers:

2

Imagine I have a UserControl that shows a parking lot (my favorite analogy) with cars of different colors. You can select a car, and in a separate UserControl (in a separate project) statistics of the selected car are displayed.

Now a user wants a button on the car statistics UC, 'Next car of same color'. When selected it should show the statistics of the next car (top to bottom, left to right) on the parking lot with that same color.

So if that makes sense, on to the question.

I am currently using MVVM Lite to send a message containing the selected car from the parking lot UC to the car statistic UC. All is good. Now though, with this new feature request, what should I do? The statistic UC needs to request the next car from the parking lot UC.

Would this be a good place to use dependency injection? Or is there another better approach?

+1  A: 

If I am getting you right, what you want is a Command with proper 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;
}

Now it is a matter of setting Button.Command="{Binding ParkingLot.ShowNextCarCommand}" now you got your control over to the ParkingLot viewmodel class and find the Next same colored car and set it again to SelectedCar property. I assume you will have RaisepropertyChanged in all these properties. I use simple DelegateCommand of Prism

Jobi Joy
That sounds like a great idea! I will give this a go first thing. Im pretty bad with commands, I just dont know what things I can do with them and what I cant. Thanks for the great example too.
Nicros
I don't understand why your Car class has a reference to the ParkingLot. It seems that Car only really needs to know its color. As the ParkingLot is basically just a list of cars (probably should be a sparse array?), only it should have the responsibility of knowing which car was selected, and which is next in line.
Dave
Yes Dave, I totally agree that the Car class doesnt look good with its awareness of ParkingLot. but I wanted to show the idea of calling another VM class's command thats all. There is a better way to get the ParkingLot dataContext at the XAML level anyway.
Jobi Joy
Oh, and just out of curiosity, doesn't his DataContext need to be set appropriately? In other words, if you're binding the Command to ParkingLot.ShowNextCarCommand, then that implies that the DataContext for the UC is a ViewModel that owns the ParkingLot class, right?
Dave
Yes somehow we should get the ParkingLot Usercontrol's datacontext to the Button. <Button DataContext="{Binding DataContext, ElementName=uc_ParkingLot}" Command="{Binding ShowNextCommand}"
Jobi Joy
Hmm, interesting. I should have taken a closer look at the code, I was thinking that the ShowNextCarCommand was more like a DP, in that it was static- but upon reflection that makes no sense.But nobody likes my idea of using dependency injection here? Still seems like it might be a good idea.
Nicros
A: 

I would use a Controller as mediator between the two ViewModels (ParkingLotViewModel and StatisticsViewModel). In your case the Controller is responsible to synchronize the selected car and it is responsible to pass the ‘Select next car of same color’ command to the ParkingLotViewModel.

The sample applications of the WPF Application Framework (WAF) show how this can work.

.

jbe

jbe