Question

I have a very frustratig problem: I have a outer View, which has a Ribbonbar at the top. There is another View, which holds 1...n Viewmodels and displays a datagrid from a List of Datatables. Which one is shown, is up to the user. The User can select a Button on the Ribbonbar, which should issue an operation on the selected rows in the Datagrid. But how to do this? I could easily launch a method on the active ViewModel, but the method then needs to get hold of all selected rows - which would violate the Idea behind MVVM. Any Ideas?

Here is a look of the Screen: http://s7.directupload.net/file/d/3228/a3m3ttu9_jpg.htm

The Button "Zeile raus", should cause an Effect in the ViewModel / Viwe contained in the lower right Tabcontrol. The Effect needs to know which rows are selected.

Était-ce utile?

La solution

Have the button publish an event from its command execute method :

public class RibbonViewModel {  
    IEventAggregator events;  

    public RibbonViewModel (IEventAggregator events){  
        this.events = events;  
    }  

    public void ButtonClickCommandExecute(){  
        events.Publish(new SomeMessage{  
            SomeNumber = 5,  
            SomeString = "Blah..."  
        });  
    }  
}

Each of your ViewModel should subscribe to this event, and react on it if it is the "active" ViewModel :

public class ViewModelWithDataGrid : IHandle<SomeMessage>{  
    public void Handle(SomeMessage message){  
        if(IsActive){           
        //do something with the message  
        }
    }  
}

This way event source is not coupled to event sink, and you can easily unit test whenever a VM should respond to an event.

Documentation : http://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator&referringTitle=Documentation

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top