0
votes

I'm attempting to create a WPF custom control that includes a reference to the unity container that has been created in the BootStrapper - e.g.

The reason being that I would like the custom control to be able to resolve the unity container to be able to use some of the services that have been registered to the container. e.g. a user preference service / an entitlements service.

So far I have created my custom control and have included in the code behind file the following dependency property

public static readonly DependencyProperty ContainerProperty = DependencyProperty.Register("Container", typeof(UnityContainer), typeof(SomeCustomWPFControl), new PropertyMetadata("DefaultTestValue"));

    public UnityContainer Container
    {
        get { return (UnityContainer)GetValue(ContainerProperty); }
        set { SetValue(ContainerProperty, value); }
    }

In the WPF form where I'm attempting to include my custom control, I've added the following line to the Resources area:

<Unity:UnityContainer x:Key="unitContainer"></Unity:UnityContainer>

In the form it's self I've attempted to create the custom control:

<Globe:SomeCustomWPFControl Container="{DynamicResource unitContainer}" DockPanel.Dock="Right" x:Name="JimEditor1" Grid.Column="0" Grid.Row="3"></Globe:SomeCustomWPFControl>

The only error information I receive at run time is

Error at object 'System.Windows.Controls.Grid' in markup file '[assembly name];[path/file].xaml' Line 135 Position 22.

Any suggestions as to where I'm going wrong? Thx.

Full Xaml:

<UserControl x:Class="DB.GPF.Globe.Views.JimTestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Globe="clr-namespace:DB.GPF.Globe" 
xmlns:Unity="clr-namespace:Microsoft.Practices.Unity;assembly=Microsoft.Practices.Unity">
<UserControl.Resources>
    <Unity:UnityContainer x:Key="unitContainer"></Unity:UnityContainer>
</UserControl.Resources>
<Grid>
    <Globe:SomeCustomWPFControl Container="{DynamicResource unitContainer}" x:Name="JImTestControl1"></Globe:SomeCustomWPFControl>
</Grid>

Update: The problem appears to be having a DependencyProperty of type UnityContainer or IUnityContainer, if the type is changed to an inbuilt .Net type, e.g. string then it runs fine. Any ideas why we can't have a DependencyProperty of type UnityContainer or IUnityContainer?

2
Can you post the rest of your Xaml?Dan Puzey
Here's the Xaml in full:Jim

2 Answers

1
votes

I think , embedding Unity in Custom Control is not a good idea.

If you need to change some properties of your custome control than expose those properties and change them

0
votes
<Globe:SomeCustomWPFControlContainer="{DynamicResource unitContainer}" DockPanel.Dock="Right" x:Name="JimEditor1" Grid.Column="0" Grid.Row="3"></Globe:SomeCustomWPFControlContainer>

There is a problem in you xaml right after <Globe:SomeCustomWPFControlContainer. You're missing a property to bind to.

EDIT: your get value is also wrong. You have GetValue(GetValue(ContextMenuProperty)) and it should be ...

public UnityContainer Container
{
    get { return (UnityContainer)GetValue(ContainerProperty); }
    set { SetValue(ContainerProperty, value); }
}