Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
35
Prism Event Aggregator public class SectionSelected : Behavior { public SectionSelected() { eventAg = new Prism.Events.EventAggregator(); } public SectionSelected(IEventAggregator ea)
posted

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);
        }

    }