Hello, since there is no bindable property in the Infragistics data grid for selected items, I am using an attached method. From the attached method I publish an event. Then the ViewModel would receive the event and record the selected items.
My problem is that the view model is not seeing the event.
I am using the event aggregator in Prism. I can not find any examples of sending an event from a plain class (not a view model) to a view model. In the view model the event aggregator is supplied by the DI container but the attached method requires a empty default constrictor, so I am constructing it there. I am not sure what the DI container is doing (perhaps a singleton?). Has anyone done this? I don't see any other clean way to get the selected items to the view model.
public class SectionSelected : Behavior<XamDataGrid> { public SectionSelected() { eventAg = new Prism.Events.EventAggregator(); } public SectionSelected(IEventAggregator ea) { eventAg = ea; } IEventAggregator eventAg; protected override void OnAttachedTo(XamDataGrid grid) { grid.SelectedItemsChanged += Grid_SelectedItemsChanged; base.OnAttachedTo(grid); } private void Grid_SelectedItemsChanged(object sender, GridSelectedItemsChangedEventArgs args) { XamDataGrid grid = (sender as XamDataGrid); if (grid.SelectedItems.Count == 1) { Section selectedItem = grid.SelectedItems[0] as Section; eventAg.GetEvent<SectionChangedEvent>().Publish(selectedItem); } else eventAg.GetEvent<SectionChangedEvent>().Publish(null); } protected override void OnDetachingFrom(XamDataGrid grid) { grid.SelectedItemsChanged -= Grid_SelectedItemsChanged; base.OnDetachingFrom(grid); } }
Hello Jim,
It appears that this issue was answered on a separate forum thread that you had submitted, here.
For consistency, I will include a transcript of the response on the other forum thread below:
"I would sugtest taking a different, more simplified approach. You should utilize the EventToCommand behavior that is built into Prism.
The docs for the feature can be found here: http://prismlibrary.github.io/docs/xamarin-forms/EventToCommandBehavior.html
You can see a sample here: https://github.com/PrismLibrary/Prism-Samples-Forms/tree/master/ContosoCookbook
The line of code you would be interested in is here: https://github.com/PrismLibrary/Prism-Samples-Forms/blob/master/ContosoCookbook/ContosoCookbook/ContosoCookbook/Views/MainPage.xaml#L25"