views:

25

answers:

1

Some UserControl uses converters, which usually look like this:

<UserControl.Resources>
    <Converters:CurrentDataConverter x:Key="CurrentDataConverter"/>
</UserControl.Resources>

I would like to have a base user control for interception of converters, for example, to use Dependency Injection. Would it be possible? Thank you.

+1  A: 

Converters (or any resource) can be declared at any level of the Xaml hierarchy so the best place to inject them is the global App resource collection (which is searched last for keys).

At any tine during startup you just add instances of the converters as name/value pairs, rather than declaring them in Xaml. That means you have full control over creation and can use an IOC container to create them.

Enough already
Thank you for the answer. When I add converters to resources before the application starts, I get a XamlParseException "Cannot find a Resource with the Name/Key...", it occures in compile time, I suppose. To avoid this I have to get all bindings in UserControl via GetBindingExpression method and define converter in code behind.
Dmitry
From memory app.xaml is processed just before the first screen loads. You could put the hook into the load event of the main outer shell and add the converters to the App.Current.Resources there *in code*. Are you using Prism or another pattern?
Enough already
I'm not using Prism, the pattern is based on lightweight MVVM.
Dmitry