Is it possible in WPF and Caliburn.Micro to control the creation of a System.Windows.Interactivity.Behavior.Behavior that I've bound in my view?
I have a situation where I want to inject a dependency via the Caliburn.Micro Bootstrapper
to one of my custom behaviors, but I really don't want resort to some kind of service locator.
My view looks like this:
<TextBox x:Name="Output">
<i:Interaction.Behaviors>
<core:ScrollBehavior />
</i:Interaction.Behaviors>
</TextBox>
and this is what I'm trying to achieve:
public class ScrollBehavior : Behavior<TextBox>
{
private readonly IMyExteneralDependency _externalDependency;
public ScrollBehavior(IMyExteneralDependency externalDependency)
{
_externalDependency = externalDependency;
}
protected override void OnAttached()
{
this.AssociatedObject.TextChanged += this.OnTextChanging;
}
protected override void OnDetaching()
{
this.AssociatedObject.TextChanged -= this.OnTextChanging;
}
private void OnTextChanging(object sender, TextChangedEventArgs e)
{
this.AssociatedObject.ScrollToEnd();
}
}
Anyone have an idea of how I can achieve this?
Unfortunately, WPF is the one in charge of the creation of the class in this scenario. As such, there isn't a direct way to use constructor injection or other similar techniques.
As such, a service locator is a common means of handling this type of scenario. Silverlight even went as far as including MEF's CompositionInitializer
for this exact type of scenario, but it was never included in WPF. I blogged a port for WPF that could be used in this type of scenario.