Users are pointing me to Unity Dependency Injection from usercontrol viewmodel to customUsercontrol Viewmodel
The top answer says Don't use DI/IoC container to inject controls
If what im doing should NOT be done, at least advise on what i SHOULD do.
I need my 2 ViewModel
to know when my grid objects are checked/unchecked, and which object it was.
How can this be done without IEventAggregator
?
I have a usercontrol
that i am using as a GridViewRow
template. This usercontrol
is not loaded until users search for something and my grid displays objects.
On the template, there are checkboxes that when checked, I need to communicate to my viewmodel
.
Currently when I build, my application works as expected.
When i modify my constructor
from public MainGridTemplate()
to public MainGridTemplate(IEventAggregator ea)
and run the app without any other code changes (not even saving to a local variable), I get the error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
My callstack only shows 1 entry, [External Code]
and my breakpoint on the constructor is never hit. This leads me to believe the IEventAggregator
that gets inject is null, or something is null along the path of injecting.
In my view model, im injecting IEventAggregator
as well as a IRepository
service and those work without issues, so why would i get this error when using DI in a usercontrol
public MainGridTemplate()
{
InitializeComponent();
PromoFromDateTextBox.DropDownOpened += HandleClicks;
PromoToDateTextBox.DropDownOpened += HandleClicks;
PromoNumberTextBox.GotFocus += HandleClicks;
PromoPriceTextBox.GotFocus += HandleClicks;
AdsCheck.Click += HandleClicks;
DistroCheck.Click += HandleClicks;
ShowCheck.Click += HandleClicks;
KeepCheck.Click += HandleClicks;
}
My template is applied with the following in xaml
<ControlTemplate x:Key="PMSMainRowTemplate"
TargetType="telerik:GridViewRow">
<template:MainGridTemplate DataContext="{Binding}" />
</ControlTemplate>
<Style TargetType="telerik:GridViewRow"
BasedOn="{StaticResource GridViewRowStyle}">
<Setter Property="Template"
Value="{StaticResource PMSMainRowTemplate}" />
</Style>
My Current Workaround:
In my bootstrapper class, ive added a property StaticContainer
public static IUnityContainer StaticContainer {get; set; }
and in my usercontrol
var container = Bootstrapper.StaticContainer;
_ea = container.Resolve<IEventAggregator>();
yay statics (not really...)